2
我有關於如何在Android中使用JSON集成的問題。例如,我有一個登錄頁面,我想使用SOAP Web服務進行身份驗證來導航它。我想使用JSON集成,我該怎麼做?有誰能夠幫助我?如果你有一個例子,請提供它。使用SOAP webservices進行Android + JSON解析
這是我的代碼,它在logcat中顯示輸出。現在如何在模擬器上檢索?
package com.temp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.kxml2.kdom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.provider.Browser;
import android.text.StaticLayout;
import android.util.Log;
import android.widget.TextView;
public class temp extends Activity{
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL ="http://";
private static final String SOAP_ACTION = "http://tempuri.org/Login";
private static final String METHOD_NAME = "Login";
TextView tv;
String string;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
string =
"{\"Geninfo\": " +
"{\"appname\":\"\"," +
"\"appver\":\"1.0.0\"," +
"\"deviceType\":\"\"," +
"\"deviceOSVersion\":\"3.0\"," +
"\"phoneDeviceID\":\"0\"}," +
"\"Login\":" +
"{\"emailID\":\"\",[email protected]"+
"\"Password\":\"123456\"}}";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false); // Comment: 1
HttpPost httppost = new HttpPost(URL); // Comment: 2
// connection.setDoOutput(true);
URL u;
URLConnection uc;
try {
u = new URL(URL);
uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
StringBuffer soap = new StringBuffer();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Host","191.118.0.4");
connection.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8");
connection.setRequestProperty("Content-Length",String.valueOf(soap.length()));
InputStream is = connection.getInputStream();
//HttpPost httpget = new HttpPost(URL);
connection.setRequestMethod(soap.toString());
//DefaultHttpClient client = new DefaultHttpClient();
// httppost.setHeader("POST","/makenewbuddies/mobileapp/APImobilelogin.asmx HTTP/1.1");
// httppost.setHeader("SOAPAction", SOAP_ACTION);
// httppost.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
// Comment: 4
soap.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">") ;
soap.append("<soap:Body>");
soap.append("<LoginRequest xmlns=\"http://tempuri.org/\">");
soap.append("<JSONRequestString>"+string+"</JSONRequestString>");
//soap.append("<strPassword>"+password+"</strPassword>");
// soap.append("<strXml_In>");
// soap.append("<XML-GetDetailsOfAMobileArticle-IN DATACLASS= 'DetailsOfAMobileArticle'> <CONDITION ARTICLE_ID= '"+articleid+"' CALLER='' USER_ID='"+userid+"'/> </XML-GetDetailsOfAMobileArticle-IN>");
// soap.append("</strXml_In>");
soap.append("</LoginRequest>");
soap.append("</soap:Body>");
soap.append("</soap:Envelope>");
Log.d("soap value1 is",soap.toString());
// String len=String.valueOf(soap.length());
// httppost.setHeader("Content-Length", String.valueOf(soap.length()));
try {
// StringEntity strent= new StringEntity(soap.toString());
// strent.setContentType("application/soap+xml; charset=utf-8");
// httppost.setEntity(strent);
ResponseHandler<String> response = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,response);
Log.v("soap response is",responseBody);
// System.out.println("Thanks God" + responseBody);
} catch(Exception e) {
e.printStackTrace();
System.out.println("Exception Thanks God : " + e.toString());
}
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(soap.toString());
wout.flush();
wout.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//InputStream in = connection.getInputStream();
String result;
//int c;
while ((result=rd.readLine()) != null) {
System.out.println(result);
} }catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
歡迎計算器。用完整的語法正確的英語句子來構建你的問題將大大提高你獲得有用答案的機會。另外,爲了得到對你的問題有用的答案,你需要更具體一些;單獨採取這些都看起來像你可以只爲谷歌的東西。 JSON解析內置於Android(搜索文檔),並且有數以千計的關於SOAP的Java教程和至少一些與Android相關的教程。 – 2011-03-08 05:14:33
soap webservice在服務器端實現。你與它無關。您只需將數據以json格式發佈到服務器,它就會對您做出迴應。 – 2011-03-08 05:14:53