java - JSON 数据错误 Android 非法参数 HttpClient

标签 java android json http-get

我在 Android 中传输 JSON 时遇到问题。我在 LogCat 中收到以下错误。

04-29 04:47:11.362: E/Trace(851): error opening trace file: No such file or directory (2)
04-29 04:47:12.243: I/System.out(851): 1
04-29 04:47:12.243: I/System.out(851): 2
04-29 04:47:12.303: W/System.err(851): java.lang.IllegalArgumentException: Illegal character in query     at index 42: http://SOME_WEBSITE/api/api.php?package=    {"key":"SOME_KEY","info":{"type":"user","login":{"password":"some_password","email":"test@gmail.    com"}},"request":"info"}
04-29 04:47:12.313: W/System.err(851):  at java.net.URI.create(URI.java:727)
04-29 04:47:12.333: W/System.err(851):  at org.apache.http.client.methods.HttpGet.<init>(HttpGet.    java:75)
04-29 04:47:12.333: W/System.err(851):  at com.example.jsontest.MainActivity.onCreate(MainActivity.    java:47)
04-29 04:47:12.333: W/System.err(851):  at android.app.Activity.performCreate(Activity.java:5104)
04-29 04:47:12.333: W/System.err(851):  at android.app.Instrumentation.callActivityOnCreate(    Instrumentation.java:1080)
04-29 04:47:12.333: W/System.err(851):  at android.app.ActivityThread.performLaunchActivity(    ActivityThread.java:2144)
04-29 04:47:12.343: W/System.err(851):  at android.app.ActivityThread.handleLaunchActivity(    ActivityThread.java:2230)
04-29 04:47:12.343: W/System.err(851):  at android.app.ActivityThread.access$600(ActivityThread.    java:141)
04-29 04:47:12.343: W/System.err(851):  at android.app.ActivityThread$H.handleMessage(ActivityThread.    java:1234)
04-29 04:47:12.343: W/System.err(851):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 04:47:12.343: W/System.err(851):  at android.os.Looper.loop(Looper.java:137)
04-29 04:47:12.343: W/System.err(851):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-29 04:47:12.343: W/System.err(851):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 04:47:12.402: W/System.err(851):  at java.lang.reflect.Method.invoke(Method.java:511)
04-29 04:47:12.402: W/System.err(851):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(    ZygoteInit.java:793)
04-29 04:47:12.402: W/System.err(851):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-29 04:47:12.402: W/System.err(851):  at dalvik.system.NativeStart.main(Native Method)
04-29 04:47:12.402: I/System.out(851): Something is wrong

这是我尝试运行的 Android 代码

public class MainActivity extends Activity 
{       
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView the_text_view = (TextView) findViewById(R.id.name);

        //Construct the JSON
        JSONObject json = new JSONObject();
        JSONObject info_json = new JSONObject();
        JSONObject login_info = new JSONObject();

        try
        {    
            login_info.put("email", "test@gmail.com");
            login_info.put("password", "some_password");

            info_json.put("type", "user");
            info_json.put("login", login_info);

            json.put("key", "SOME_KEY");
            json.put("request", "info");
            json.put("info", info_json);

            HttpClient httpClient = new DefaultHttpClient();
            System.out.println("1");
            String requestLink = "http://SOME_WEBSITE/api/api.php?package="+json.toString();
            System.out.println("2");        
            HttpGet httpGet = new HttpGet(requestLink);
            System.out.println("3");
            HttpResponse httpResponseGet = httpClient.execute(httpGet);
            System.out.println("4");
            HttpEntity resEntityGet = httpResponseGet.getEntity();
            System.out.println("5");

            if (resEntityGet != null) 
            {
                String response = EntityUtils.toString(resEntityGet);             
                JSONObject json_response = new JSONObject(response);             
                System.out.println(json.toString());
            }

            System.out.println("Looks alright");
        }
        catch (Exception e) 
        {
            e.printStackTrace();  
            System.out.println("Something is wrong");
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

它必须与 GET 调用有关。我不确定它到底是什么,但我所有的 System.out 都打印到程序的那部分...

在此先感谢您的帮助!

最佳答案

首先 您正在传递一个包含空格的查询字符串,这会破坏 url。因此,您需要在发送前进行编码:

String requestLink = "http://SOME_WEBSITE/api/api.php?package="+Encoder.encode(json.toString(),"UTF-8");

第二

您正在尝试在 UI 线程上执行网络操作,这在 Android 3.0+ 中是不允许的,并且会抛出 NetworkOnMainThreadException。使用 AsyncTask 或单独的线程来执行网络操作。

关于java - JSON 数据错误 Android 非法参数 HttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271451/

相关文章:

java - 使用 hibernate 条件 hibernate 组中的最后一条记录

java - Android WebRTC - 为 session 混合音轨

java - 使用数据库进行 Intent 传递的问题

javascript - 我的 javascript 代码有什么错误?

java - 仅解析大型 JSON 字符串中的一个字段

java - gradle checkstyle 只是输出温暖,但构建成功

java - 泛型编程是多态性的一个例子吗?

Android - ListView 重复事件

android - JSON文件VS SQLite android

java - XML/Xpath -- getTextContent() -- 我希望将每个属性作为字符串获取