Method Not Supported Error On Parsing Json.
Solution 1:
You're trying to POST
to a resource that only supports GET
.
Try using an HttpGet
rather than an HttpPost
.
Solution 2:
You should use Get
request instead of POST
request.
publicclassMainActivityextendsActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make async request newmyAsynctask().execute();
}
//********************************************//get json stringpublic String getJSONString() {
StringBuilderbuilder=newStringBuilder();
HttpClientclient=newDefaultHttpClient();
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";
HttpGethttpGet=newHttpGet(urlString);
try {
HttpResponseresponse= client.execute(httpGet);
StatusLinestatusLine= response.getStatusLine();
intstatusCode= statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntityentity= response.getEntity();
InputStreamcontent= entity.getContent();
BufferedReaderreader=newBufferedReader(newInputStreamReader(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();
}
//********************************************privateclassmyAsynctaskextendsAsyncTask<Void, Void, String>{
@Overrideprotected String doInBackground(Void... params) {
String jsonString=getJSONString();
return jsonString;
}
@OverrideprotectedvoidonPostExecute(String jsonString) {
super.onPostExecute(jsonString);
Log.e(getClass().getSimpleName(), jsonString);
}
}
}
Solution 3:
jParser.getJSONFromUrl(url) uses POST method but you should use GET
Solution 4:
The error you receive comes from the server, as it is not supporting POST for that actual URLs.
It is good practice to check the HTTP status code before processing the result. Do this by calling int statusCode = httpResponse.getStatusLine().getStatusCode();
. The status code 2XX means success (Codes listed here).
Another problem with your code is that though you are creating a POST request, all information is provided in the query parameters for the request. The POST part (body part) of the request is actually empty.
MultipartEntityentity=newMultipartEntity();
entity.addPart("alma", newStringBody("beka", Charset.forName("UTF-8")));
entity.addPart("apple", newStringBody("frog", Charset.forName("UTF-8")));
httpPost.setEntity(entity);
This is an example of adding the parameters as POST parameters to the request...
Post a Comment for "Method Not Supported Error On Parsing Json."