每当我试图打开这个应用程序时,我都会收到此错误。 我正在解析来自我本地主机的数据。我没有得到什么是错误..我是android的新手,我也不擅长阅读logcat 任何人都可以解决这个问题....在此之前感谢...
MainActivity:
package com.newjson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private static String url = "http://10.0.2.2:80/sample/";
private static final String TAG_RESPONSE = "response";
private static final String TAG_NAME = "RetailerName";
private static final String TAG_EMAIL = "Email";
private static final String TAG_PHONE = "Phone";
private static final String TAG_ADDRESS = "Address";
JSONArray response = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JSONParser parser = new JSONParser();
JSONObject jobj = parser.getJSONFromUrl(url);
try{
response = jobj.getJSONArray(TAG_RESPONSE);
JSONObject c = response.getJSONObject(0);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String phone = c.getString(TAG_PHONE);
String address = c.getString(TAG_ADDRESS);
TextView name1 = (TextView)findViewById(R.id.name);
TextView email1 = (TextView)findViewById(R.id.email);
TextView phone1 = (TextView)findViewById(R.id.phone);
TextView address1 = (TextView)findViewById(R.id.address);
name1.setText(name);
email1.setText(email);
phone1.setText(phone);
address1.setText(address);
}catch(JSONException e){
e.printStackTrace();
}
}
}
JSONParser :
package com.newjson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jobj = null;
static String json = "";
public JSONParser(){
}
public JSONObject getJSONFromUrl(String url){
try{
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost();
HttpResponse respose = client.execute(post);
HttpEntity entity = respose.getEntity();
is = entity.getContent();
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
try{
BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine())!= null){
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch(Exception e){
Log.e("buffer error", "error converting result"+e.toString());
}
return jobj;
}
}
logcat的:
11-26 12:15:50.296: D/AndroidRuntime(639): Shutting down VM
11-26 12:15:50.296: W/dalvikvm(639): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-26 12:15:50.330: E/AndroidRuntime(639): FATAL EXCEPTION: main
11-26 12:15:50.330: E/AndroidRuntime(639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.newjson/com.newjson.MainActivity}: java.lang.NullPointerException
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.os.Looper.loop(Looper.java:123)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-26 12:15:50.330: E/AndroidRuntime(639): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 12:15:50.330: E/AndroidRuntime(639): at java.lang.reflect.Method.invoke(Method.java:507)
11-26 12:15:50.330: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-26 12:15:50.330: E/AndroidRuntime(639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-26 12:15:50.330: E/AndroidRuntime(639): at dalvik.system.NativeStart.main(Native Method)
11-26 12:15:50.330: E/AndroidRuntime(639): Caused by: java.lang.NullPointerException
11-26 12:15:50.330: E/AndroidRuntime(639): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:496)
11-26 12:15:50.330: E/AndroidRuntime(639): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-26 12:15:50.330: E/AndroidRuntime(639): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-26 12:15:50.330: E/AndroidRuntime(639): at com.newjson.JSONParser.getJSONFromUrl(JSONParser.java:31)
11-26 12:15:50.330: E/AndroidRuntime(639): at com.newjson.MainActivity.onCreate(MainActivity.java:32)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-26 12:15:50.330: E/AndroidRuntime(639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-26 12:15:50.330: E/AndroidRuntime(639): ... 11 more
这是永远存在的:
答案 0 :(得分:0)
你的post变量在这一行中抛出空指针异常
HttpPost post = new HttpPost();
只需将网址添加为参数
即可 HttpPost post = new HttpPost(url);
答案 1 :(得分:0)
请勿使用HttpPost
,您要做的事情是HttpGet
。使用;
getJSONFromUrl
方法
public JSONObject getJSONFromUrl(String url){
try{
HttpParams httpParameters = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGet(url);
HttpResponse respose = client.execute(httpget);
HttpEntity entity = respose.getEntity();
is = entity.getContent();
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
try{
BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine())!= null){
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch(Exception e){
Log.e("buffer error", "error converting result"+e.toString());
}
return jobj;
}
最后,您必须在AsyncTask上进行网络操作。使用;
更改您的MainActivity代码public class MainActivity extends Activity {
private static String url = "http://10.0.2.2:80/sample/";
private static final String TAG_RESPONSE = "response";
private static final String TAG_NAME = "RetailerName";
private static final String TAG_EMAIL = "Email";
private static final String TAG_PHONE = "Phone";
private static final String TAG_ADDRESS = "Address";
private ProgressDialog progressDialog;
private YourTask task;
JSONArray response = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
task = new YourTask();
progressDialog = ProgressDialog.show(MainActivity.this, "", "Downloading...",true ,false);
task.execute();
}
public class YourTask extends AsyncTask<Void, Void, JSONObject>{
@Override
protected Void doInBackground(Void... arg0) {
JSONParser parser = new JSONParser();
JSONObject jobj = parser.getJSONFromUrl(url);
return jobj;
}
@Override
protected void onPostExecute(JSONObject jobj) {
try{
response = jobj.getJSONArray(TAG_RESPONSE);
JSONObject c = response.getJSONObject(0);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String phone = c.getString(TAG_PHONE);
String address = c.getString(TAG_ADDRESS);
TextView name1 = (TextView)findViewById(R.id.name);
TextView email1 = (TextView)findViewById(R.id.email);
TextView phone1 = (TextView)findViewById(R.id.phone);
TextView address1 = (TextView)findViewById(R.id.address);
name1.setText(name);
email1.setText(email);
phone1.setText(phone);
address1.setText(address);
}catch(JSONException e){
e.printStackTrace();
}
progressDialog.dismiss();
}
@Override
protected void onCancelled() {
task = null;
progressDialog.dismiss();
}
}
}