JSON中的未知错误

时间:2014-07-04 05:16:52

标签: android json

嗨,这是我第一次使用JSON,我收到了这个错误

PLZ有人可以告诉我如何修复它是我的代码:

MainActivity:

private static String url = "";

//JSON Node Names 
private static final String TAG_OS = "android";
private static final String TAG_VER = "ver";
private static final String TAG_NAME = "name";
private static final String TAG_API = "api";

JSONArray android = null;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    oslist = new ArrayList<HashMap<String, String>>();



    Btngetdata = (Button)findViewById(R.id.getdata);
    Btngetdata.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
        new JSONParse().execute();


        }
    });


}



private class JSONParse extends AsyncTask<String, String, JSONObject> {
     private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         ver = (TextView)findViewById(R.id.vers);
         name = (TextView)findViewById(R.id.name);
         api = (TextView)findViewById(R.id.api);
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();



    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                JSONObject c = android.getJSONObject(i);

                // Storing  JSON item in a Variable
                String ver = c.getString(TAG_VER);
                String name = c.getString(TAG_NAME);
                String api = c.getString(TAG_API);




                // Adding value HashMap key => value


                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_VER, ver);
                map.put(TAG_NAME, name);
                map.put(TAG_API, api);

                oslist.add(map);
                list=(ListView)findViewById(R.id.list);





                ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
                                R.id.vers,R.id.name, R.id.api});

                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();

                    }
                });

                }
        } catch (JSONException e) {
            e.printStackTrace();
        }


     }
}

}

这是JSONParser

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object

    // return JSON String
    try {
        return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return jObj;

//返回新的JSONObject(json.substring(json.indexOf(“{”),json.lastIndexOf(“}”)+ 1));     } }

和我的logcat:

07-04 07:54:39.954: E/AndroidRuntime(15437): FATAL EXCEPTION: AsyncTask #2
07-04 07:54:39.954: E/AndroidRuntime(15437): java.lang.RuntimeException: An error occured while executing doInBackground()
07-04 07:54:39.954: E/AndroidRuntime(15437):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.lang.Thread.run(Thread.java:856)
07-04 07:54:39.954: E/AndroidRuntime(15437): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=
07-04 07:54:39.954: E/AndroidRuntime(15437):    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at learn2crack.listview.library.JSONParser.getJSONFromUrl(JSONParser.java:38)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at com.bsmat.manal.MainActivity$JSONParse.doInBackground(MainActivity.java:100)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at com.bsmat.manal.MainActivity$JSONParse.doInBackground(MainActivity.java:1)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-04 07:54:39.954: E/AndroidRuntime(15437):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-04 07:54:39.954: E/AndroidRuntime(15437):    ... 5 more
07-04 07:54:50.294: E/Ads(15437): Not enough space to show ad! Wants: <936, 120>, Has: <720, 1280>
07-04 07:54:50.299: E/Ads(15437): Not enough space to show ad! Wants: <936, 120>, Has: <720, 1280>
07-04 07:54:50.374: E/Ads(15437): Not enough space to show ad! Wants: <936, 120>, Has: <720, 1280>
07-04 07:54:50.374: E/Ads(15437): Not enough space to show ad! Wants: <936, 120>, Has: <720, 1280>
07-04 07:54:50.439: E/WindowManager(15437): Activity com.bsmat.manal.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41d01e58 that was originally added here
07-04 07:54:50.439: E/WindowManager(15437): android.view.WindowLeaked: Activity com.bsmat.manal.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41d01e58 that was originally added here
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:402)
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:311)
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.Window$LocalWindowManager.addView(Window.java:554)
07-04 07:54:50.439: E/WindowManager(15437):     at android.app.Dialog.show(Dialog.java:277)
07-04 07:54:50.439: E/WindowManager(15437):     at com.bsmat.manal.MainActivity$JSONParse.onPreExecute(MainActivity.java:88)
07-04 07:54:50.439: E/WindowManager(15437):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
07-04 07:54:50.439: E/WindowManager(15437):     at android.os.AsyncTask.execute(AsyncTask.java:534)
07-04 07:54:50.439: E/WindowManager(15437):     at com.bsmat.manal.MainActivity$1.onClick(MainActivity.java:65)
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.View.performClick(View.java:4211)
07-04 07:54:50.439: E/WindowManager(15437):     at android.view.View$PerformClick.run(View.java:17267)
07-04 07:54:50.439: E/WindowManager(15437):     at android.os.Handler.handleCallback(Handler.java:615)
07-04 07:54:50.439: E/WindowManager(15437):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 07:54:50.439: E/WindowManager(15437):     at android.os.Looper.loop(Looper.java:137)
07-04 07:54:50.439: E/WindowManager(15437):     at android.app.ActivityThread.main(ActivityThread.java:4898)
07-04 07:54:50.439: E/WindowManager(15437):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 07:54:50.439: E/WindowManager(15437):     at java.lang.reflect.Method.invoke(Method.java:511)
07-04 07:54:50.439: E/WindowManager(15437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-04 07:54:50.439: E/WindowManager(15437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-04 07:54:50.439: E/WindowManager(15437):     at dalvik.system.NativeStart.main(Native Method)
07-04 07:54:50.459: E/Ads(15437): Not enough space to show ad! Wants: <936, 120>, Has: <720, 1280>
07-04 07:54:50.459: E/Ads(15437): Not enough space to show ad! Wants: <936, 120>, Has: <720, 1280>

1 个答案:

答案 0 :(得分:0)

例外说

  

引起:java.lang.IllegalStateException:目标主机不能是   null,或在参数中设置。 scheme = null,host = null,path =

表示您的主机为空。你忘了指定网址。你的url变量设置为null.please pass url here

JSONObject json = jParser.getJSONFromUrl(url);

或将您的网址设置为某个内容。