【问题标题】:Method not supported error on parsing JSON.解析 JSON 时方法不支持错误。
【发布时间】:2012-10-29 12:27:45
【问题描述】:

我正在尝试 json 解析,但是当解析时出现错误 此网址不支持 httppost 方法 我把我的代码放在这里

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

 public class SearchlistActivity extends Activity {

    private static String 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/";   private static final String TAG_RESULTS = "results";    private static final String TAG_RESPONSEDATA="responseData";    

        @Override
    public void onCreate(Bundle savedInstanceState)

    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.e("json ----","urlll---->"+url);       
        //JSONArray results = null;       
         JSONArray responseData=null;
     // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        JsonParserSearch jParser = new JsonParserSearch();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts

             responseData = json.getJSONArray(TAG_RESPONSEDATA);
             Log.e("jsonnn data","sfsssss00000-------->"+responseData);

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

    } }

> 

Jsonparsersearch.java

公共类 JsonParserSearch { static InputStream is = null; 静态 JSONObject jObj = null; 静态字符串 json = "";

// 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)

【问题讨论】:

    标签: android json


    【解决方案1】:

    您正在尝试将POST 转到仅支持GET 的资源。

    尝试使用HttpGet 而不是HttpPost

    【讨论】:

      【解决方案2】:

      jParser.getJSONFromUrl(url) 使用 POST 方法,但你应该使用 GET

      【讨论】:

        【解决方案3】:

        您收到的错误来自服务器,因为它不支持该实际 URL 的 POST。

        最好在处理结果之前检查 HTTP 状态代码。通过调用int statusCode = httpResponse.getStatusLine().getStatusCode(); 来执行此操作。状态码 2XX 表示成功(列出的代码here)。

        您的代码的另一个问题是,虽然您正在创建一个 POST 请求,但所有信息都在请求的查询参数中提供。请求的POST部分(body部分)其实是空的。

        MultipartEntity entity = new MultipartEntity();
        entity.addPart("alma", new StringBody("beka", Charset.forName("UTF-8")));
        entity.addPart("apple", new StringBody("frog", Charset.forName("UTF-8")));
        httpPost.setEntity(entity);
        

        这是一个将参数作为 POST 参数添加到请求中的示例...

        【讨论】:

          【解决方案4】:

          您应该使用Get 请求而不是POST 请求。

          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);
              }
          }
          

          }

          【讨论】:

            猜你喜欢
            • 2017-05-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-19
            • 2011-02-14
            • 2022-06-25
            • 2013-04-17
            相关资源
            最近更新 更多