我试图通过此代码解析JSON url,并且每次尝试运行此应用程序时都会崩溃。 在主要活动中我调用ServiceHandler类,它可以在互联网上从JSON Url中获取字符串。
package com.example.parseable;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
public class MainActivity extends Activity {
private static String strurl=***; /**url cannot be disclosed, JSON on url is=[{"Pan_Number":"PV2398P","UserName":"DIVYA VERMA","RMContactNumber":"77867651"}] **/
JSONArray ja;
String pan;
String name;
String rm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
TextView panno=(TextView) findViewById(R.id.panx);
TextView uname=(TextView) findViewById(R.id.namex);
TextView rmno=(TextView) findViewById(R.id.rmx);
ServiceHandler sh=new ServiceHandler();
String jsonstr=sh.makeServiceCall(strurl, ServiceHandler.GET);
if(jsonstr!=null){
try{
ja=new JSONArray(jsonstr);
JSONObject jo=new JSONObject();
jo=ja.getJSONObject(0);
pan=jo.getString("Pan_Number");
name=jo.getString("UserName");
rm=jo.getString("RMContactNumber");
} catch(JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the specified url");
}
panno.setText(pan);
uname.setText(name);
rmno.setText(rm);
}
}
and ServiceHandler Class
package com.example.parseable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}