当前位置: 首页 > 知识库问答 >
问题:

解析 JSON 时的方法不支持的错误。

武晨
2023-03-14

我正在尝试json解析,但当解析时,然后给出错误httppost方法不受此网址支持,我在这里放下我的代码

http://ajax.googleapis.com/ajax/services/search/local?v=1.0

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)

共有3个答案

哈和惬
2023-03-14

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

丌官向荣
2023-03-14

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

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

}

公羊浩阔
2023-03-14

您正在尝试对仅支持GET的资源进行POST。

尝试使用HttpGet而不是HttpPost

 类似资料:
  • 本文向大家介绍AngularJS在IE8的不支持的解决方法,包括了AngularJS在IE8的不支持的解决方法的使用技巧和注意事项,需要的朋友参考一下 AngularJS一般不会选择IE8支持, 因为很多特性在IE8下效果很差, 性能也不好, 但是由于项目的需要, 客户的机器有些是XP, 只能够装IE8, 所以为了解决这个, 我查阅了相关的资料,发现GITHUT有一些对AngularJS的改进,我

  • 我得到以下错误:归档条目是使用不支持的压缩方法压缩的。我要解码以下gzip压缩的base64字符串,下面是字符串: 这和问题有关吗? 下面是我的代码:

  • 当我试图通过liquibase命令行启动changelog.groovy时,它告诉我 我正在执行下一个操作: 我的LiquiBase.Properties是: driver=org.postgresql.driver classpath=c:\users\andrii\org.postgresql.driver.jar;c:\users\andrii\liquibase-3.5.1-bin\lib

  • 我试图对我的图像进行预处理,以便最终从预处理后的图像中找到轮廓。 它给了我一个错误说明:OpenCV错误:cvStartFindContours中不支持的格式或格式组合([Start]FindContours在模式下仅支持CV_8UC1图像!=CV_RETR_FLOODFILL,否则仅支持CV_32SC1图像) 我用的是opencv3.0

  • 本文向大家介绍vbs 解析json jsonp的方法,包括了vbs 解析json jsonp的方法的使用技巧和注意事项,需要的朋友参考一下 我收集了三种常见 json jsonp 的格式,因为很多人找我说如何用正则提取某个字符串,我看了下,要么是json,要么是jsonp, 正则简直浪费,比如那种空间里说说的数据,上百条的信息,你正则个P,而且还有回复里的格式也差不多的,所以最好的方法还是解析。

  • 本文向大家介绍Java中Json解析的方法分析,包括了Java中Json解析的方法分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java中Json解析的方法。分享给大家供大家参考,具体如下: 首先准备一个JSON格式的字符串 下面是一个Json解析的程序代码 PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用: 在线JSON代码检验、检验、美化、格式