android解析json错误接受连接,中止

时间:2015-09-01 11:14:41

标签: android json eclipse

我按照本教程android-json-parser-from-url学习如何使用android应用程序解析json。

但是当试图执行我的应用程序时,eclipse给了我这个错误:

在模拟器上接受连接,中止和找不到应用程序时出错。

这是我的代码:

1)主要扩展活动

  package com.example.expensesharing;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class Main extends Activity {

JSONParser jsonparser = new JSONParser();
TextView tv;
String ab;
JSONObject jobj = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);
    new retrievedata().execute();

}


class retrievedata extends AsyncTask<String,String,String>{

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        jobj = jsonparser.makeHttpRequest("https://bugzilla.mozilla.org/rest/bug/35");

        // check your log for json response
        Log.d("Login attempt", jobj.toString());

        try {
            ab = jobj.getString("key");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ab;
    }
    protected void onPostExecute(String ab){

        tv.setText(ab);
    }

}

}

2)JSonParser.java

 package com.example.expensesharing;
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.JSONException;
import org.json.JSONObject;

public class JSONParser {
    static InputStream is = null;
    static JSONObject jobj = null;
    static String json = "";
    public JSONParser(){

    }
    public JSONObject makeHttpRequest(String url){
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            try {
                HttpResponse httpresponse = httpclient.execute(httppost);
                HttpEntity httpentity = httpresponse.getEntity();
                is = httpentity.getContent();

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                try {
                    while((line = reader.readLine())!=null){
                        sb.append(line+"\n");   

                    }
                    is.close();
                    json = sb.toString();
                    try {
                        jobj = new JSONObject(json);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return jobj;

    }

}

这是我的xml布局

       <RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text=" " />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"
        android:layout_marginTop="71dp"
        android:layout_toLeftOf="@+id/tv"
        android:text="TextView" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

确保您没有运行其他任何虚拟机  重启AVD