HtttpResponseException:通过JSON将输入数据传递给服务器

时间:2021-09-07 18:14:55

Please have a look at the following code

请查看以下代码

   package com.example.jsontest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText editText;

/** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    StrictMode.ThreadPolicy policy = new StrictMode.
    ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 

    setContentView(R.layout.activity_main);

    editText = (EditText)findViewById(R.id.edit_text);

    //Call The JSon
    try {
        //Get the JSON
        //JSONObject jObject = new JSONObject(getJson());

        //editText.append(jObject.toString());

    //  confidence
        //Put the JSON
        JSONObject getJsonObject = new JSONObject(putJson());
        //editText.append(getJsonObject.getString("created"));


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace(); 
    }

  }



  private String putJson()
  {
      DefaultHttpClient  client = new DefaultHttpClient();

      StringBuffer sb = null;
      try {
          String postURL = "https://bigml.io/andromeda/prediction?username=XXXXXXXX;api_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXX";

          HttpPost req = new HttpPost(postURL);
          req.setHeader("Content-Type", "application/json");


          JSONObject requestObject = new JSONObject();
          requestObject.put("model",  "model/xxxxxxxxxxx");
          requestObject.put("input_data",  "000008:5");

          StringEntity reqEntity = new StringEntity(requestObject.toString());
          req.setEntity(reqEntity);

          HttpResponse res = client.execute(req);
          HttpEntity entity = res.getEntity();
          String responseString = new BasicResponseHandler().handleResponse(res);
          if (entity != null) {
              //EntityUtils.consume(entity);
          }

          sb = new StringBuffer("");


          sb.append(responseString);

      } catch (Exception ex) {
          ex.printStackTrace();
          sb = new StringBuffer(ex.toString());
      }

      return sb.toString();

  }
} 

First, this is a test code, that is why the network security has been overridden.

首先,这是一个测试代码,这就是为什么网络安全被覆盖的原因。

I am trying to send data into BigML.com to get a prediction. According to theor prediction document, the data should be passed like this (see the below image) and this is the page - https://bigml.com/developers/predictions

我正在尝试将数据发送到BigML.com以获得预测。根据理论预测文档,数据应该像这样传递(见下图),这是页面 - https://bigml.com/developers/predictions

HtttpResponseException:通过JSON将输入数据传递给服务器

Now, whenever I run my code, I get the following error

现在,每当我运行我的代码时,我都会收到以下错误

12-18 10:09:09.880: W/System.err(1331): org.apache.http.client.HttpResponseException: NOT FOUND
12-18 10:09:09.880: W/System.err(1331):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
12-18 10:09:09.889: W/System.err(1331):     at com.example.jsontest.MainActivity.putJson(MainActivity.java:124)
12-18 10:09:09.889: W/System.err(1331):     at com.example.jsontest.MainActivity.onCreate(MainActivity.java:58)
12-18 10:09:09.889: W/System.err(1331):     at android.app.Activity.performCreate(Activity.java:5104)
12-18 10:09:09.900: W/System.err(1331):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-18 10:09:09.900: W/System.err(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-18 10:09:09.910: W/System.err(1331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-18 10:09:09.910: W/System.err(1331):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-18 10:09:09.920: W/System.err(1331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-18 10:09:09.920: W/System.err(1331):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 10:09:09.920: W/System.err(1331):     at android.os.Looper.loop(Looper.java:137)
12-18 10:09:09.943: W/System.err(1331):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-18 10:09:09.943: W/System.err(1331):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 10:09:09.950: W/System.err(1331):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 10:09:09.950: W/System.err(1331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-18 10:09:09.950: W/System.err(1331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-18 10:09:09.960: W/System.err(1331):     at dalvik.system.NativeStart.main(Native Method)
12-18 10:09:09.980: W/System.err(1331): org.json.JSONException: Value org.apache.http.client.HttpResponseException of type java.lang.String cannot be converted to JSONObject
12-18 10:09:09.980: W/System.err(1331):     at org.json.JSON.typeMismatch(JSON.java:111)
12-18 10:09:10.000: W/System.err(1331):     at org.json.JSONObject.<init>(JSONObject.java:158)
12-18 10:09:10.030: W/System.err(1331):     at org.json.JSONObject.<init>(JSONObject.java:171)
12-18 10:09:10.030: W/System.err(1331):     at com.example.jsontest.MainActivity.onCreate(MainActivity.java:58)
12-18 10:09:10.030: W/System.err(1331):     at android.app.Activity.performCreate(Activity.java:5104)
12-18 10:09:10.040: W/System.err(1331):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-18 10:09:10.050: W/System.err(1331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-18 10:09:10.050: W/System.err(1331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-18 10:09:10.093: W/System.err(1331):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-18 10:09:10.093: W/System.err(1331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-18 10:09:10.100: W/System.err(1331):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 10:09:10.110: W/System.err(1331):     at android.os.Looper.loop(Looper.java:137)
12-18 10:09:10.120: W/System.err(1331):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-18 10:09:10.120: W/System.err(1331):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 10:09:10.153: W/System.err(1331):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 10:09:10.160: W/System.err(1331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-18 10:09:10.160: W/System.err(1331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-18 10:09:10.171: W/System.err(1331):     at dalvik.system.NativeStart.main(Native Method)

I know the authentication data are correct because if I simply paste the url https://bigml.io/andromeda/prediction?username=xxx;api_key=xxxxxxxxxxxxxx" to the browser, then I get an output of prediction data, which was created "without" any input data.

我知道验证数据是正确的,因为如果我只是将网址https://bigml.io/andromeda/prediction?username=xxx;api_key=xxxxxxxxxxxxxx“粘贴到浏览器中,那么我会获得预测数据的输出,这是创建的“没有”任何输入数据。

What am I doing here wrong?

我在这做错了什么?

3 个解决方案

#1


0  

It seems your request is getting an exceception.

看来你的要求是一个例外。

This piece of code:

这段代码:

 } catch (Exception ex) {
      ex.printStackTrace();
      sb = new StringBuffer(ex.toString());
  }

  return sb.toString();

Lets your method return the Exception's name. If yout put a log statement in your catch you can see what exactly is going wrong:

让您的方法返回Exception的名称。如果你在catch中放入一个日志语句,你可以看到到底出了什么问题:

 } catch (Exception ex) {
      Log.e("MY_TAG", "Something went wrong here", ex);
      ex.printStackTrace();
      sb = new StringBuffer(ex.toString());
  }

  return sb.toString();

You can check LogCat to see exactly what's going wrong.

您可以检查LogCat以确切了解出现了什么问题。

#2


-1  

org.json.JSONException: Value org.apache.http.client.HttpResponseException of type java.lang.String cannot be converted to JSONObject

this simply means response which you are getting cannot be converted into JSON, check if string you are returning is a valid JSON string.

这只是意味着您获得的响应无法转换为JSON,请检查您返回的字符串是否是有效的JSON字符串。

#3


-1  

Are you doing Network IO on MainThread.? It isn't allowed..

你在MainThread上做网络IO吗?不允许..

#1


0  

It seems your request is getting an exceception.

看来你的要求是一个例外。

This piece of code:

这段代码:

 } catch (Exception ex) {
      ex.printStackTrace();
      sb = new StringBuffer(ex.toString());
  }

  return sb.toString();

Lets your method return the Exception's name. If yout put a log statement in your catch you can see what exactly is going wrong:

让您的方法返回Exception的名称。如果你在catch中放入一个日志语句,你可以看到到底出了什么问题:

 } catch (Exception ex) {
      Log.e("MY_TAG", "Something went wrong here", ex);
      ex.printStackTrace();
      sb = new StringBuffer(ex.toString());
  }

  return sb.toString();

You can check LogCat to see exactly what's going wrong.

您可以检查LogCat以确切了解出现了什么问题。

#2


-1  

org.json.JSONException: Value org.apache.http.client.HttpResponseException of type java.lang.String cannot be converted to JSONObject

this simply means response which you are getting cannot be converted into JSON, check if string you are returning is a valid JSON string.

这只是意味着您获得的响应无法转换为JSON,请检查您返回的字符串是否是有效的JSON字符串。

#3


-1  

Are you doing Network IO on MainThread.? It isn't allowed..

你在MainThread上做网络IO吗?不允许..