本文介绍了法不支持JSON解析错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力的JSON解析,但是当解析则给出错误
httppost方法不受此URL支持
我在这里放下我的code

<一个href=\"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurants&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json\" rel=\"nofollow\">http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurants&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json

  SearchListActivity.java 公共类SearchlistActivity延伸活动{    私人静态字符串url=\"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurants&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json/\";私有静态最后弦乐TAG_RESULTS =成果;私有静态最后弦乐TAG_RESPONSEDATA =responseData;        @覆盖
    公共无效的onCreate(捆绑savedInstanceState)    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        Log.e(JSON ----,urlll ----&gt;中+网址);
        // JSONArray结果= NULL;
         JSONArray responseData = NULL;
     //为哈希映射的ListView
        ArrayList的&LT;&HashMap的LT;字符串,字符串&GT;&GT; contactList =新的ArrayList&LT;&HashMap的LT;字符串,字符串&GT;&GT;();        //创建JSON解析器实例
        JsonParserSearch jParser =新JsonParserSearch();        // URL从获取JSON字符串
        JSONObject的JSON = jParser.getJSONFromUrl(URL);        尝试{
            //获取联系人的数组             responseData = json.getJSONArray(TAG_RESPONSEDATA);
             Log.e(jsonnn数据,sfsssss00000 --------&gt;中+ responseData);            }
        赶上(JSONException E){
            e.printStackTrace();
        }    }}&GT;

// constructor
public JsonParserSearch() {

}

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, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

       Log.e("json object url","display"+json);


    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser finaal", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}      }

 >     > > lOGCAT ERROR
    >     > > 
    >     > > 11-09 12:23:17.700: E/json ----(481): urlll---->http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurants&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json 11-09 12:23:18.260: E/json object url(481): display<HTML> 11-09
    >     > 12:23:18.260: E/json object url(481): <HEAD> 11-09 12:23:18.260:
    >     > E/json object url(481): <TITLE>HTTP method POST is not supported by
    >     > this URL</TITLE> 11-09 12:23:18.260: E/json object url(481): </HEAD>
    >     > 11-09 12:23:18.260: E/json object url(481): <BODY BGCOLOR="#FFFFFF"
    >     > TEXT="#000000"> 11-09 12:23:18.260: E/json object url(481): <H1>HTTP
    >     > method POST is not supported by this URL</H1> 11-09 12:23:18.260:
    >     > E/json object url(481): <H2>Error 405</H2> 11-09 12:23:18.260: E/json
    >     > object url(481): </BODY> 11-09 12:23:18.260: E/json object url(481):
    >     > </HTML> 11-09 12:23:18.260: E/JSON Parser finaal(481): Error parsing
    >     > data org.json.JSONException: Value <HTML> of type java.lang.String
    >     > cannot be converted to JSONObject 11-09 12:23:18.270:
    >     > D/AndroidRuntime(481): Shutting down VM 11-09 12:23:18.270:
    >     > W/dalvikvm(481): threadid=1: thread exiting with uncaught exception
    >     > (group=0x4001d800) 11-09 12:23:18.290: E/AndroidRuntime(481): FATAL
    >     > EXCEPTION: main 11-09 12:23:18.290: E/AndroidRuntime(481):
    >     > java.lang.RuntimeException: Unable to start activity
    >     > ComponentInfo{com.example.ssss/com.example.ssss.SearchlistActivity}:
    >     > java.lang.NullPointerException 11-09 12:23:18.290:
    >     > E/AndroidRuntime(481):  at
    >     > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    >     > 11-09 12:23:18.290: E/AndroidRuntime(481):  at
    >     > android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    >     > 11-09 12:23:18.290: E/AndroidRuntime(481):  at
    >     > android.app.ActivityThread.access$2300(ActivityThread.java:125)
解决方案

You should use Get request instead of POST request.

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //make async request 
    new myAsynctask().execute();
}

//********************************************
//get json string
public String getJSONString() {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();

    String urlString="http://www.ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurants&rsz=8&sll=-27.5595451,-48.6206452&radius=1000&output=json";

    HttpGet httpGet = new HttpGet(urlString);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e(getClass().getSimpleName(), "Failed to download json response");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}
//********************************************
private class myAsynctask extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... params) {

        String jsonString=getJSONString();
        return jsonString;
    }
    @Override
    protected void onPostExecute(String jsonString) {
        super.onPostExecute(jsonString);
        Log.e(getClass().getSimpleName(), jsonString);
    }
}

}

这篇关于法不支持JSON解析错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:29