我有一个关于如何在Android中使用JSON集成的问题。例如,我有一个登录页面,我希望使用SOAP webservices进行身份验证以进行导航。我想使用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\":\"\",abc@gmailcom"+
"\"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();
}
}
}
答案 0 :(得分:2)
Android SDK包含JSON实现。您在body被格式化为JSON时提交请求。如果该请求被接受,您的服务将返回响应,其中响应的主体将采用JSON格式。之后,您只需通过声明JSON对象进行解析,如下所示:
JSONObject json = new JSONObject(rawJson);
在此处查看更多http://goo.gl/iVXI4或仅查看Google