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