Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with JSON data Android Illegal Argument HttpClient

I'm having trouble with JSON transfer in Android. I receive the following error in 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

This the Android code that I am trying to run

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", "[email protected]");
            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;
    }
}

It has to be something with the GET call. I'm not sure what exactly it is, but all my System.out print up to that part of the program...

Thanks in advance for any help!

like image 485
nathansizemore Avatar asked Sep 14 '26 16:09

nathansizemore


1 Answers

First You are passing a query string that has spaces in it, which will break the url. Tehrefore, you need to perform an encode before sending:

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

Second

You are trying to perform network operation on UI Thread, which will not be allowed in Android 3.0+ and will throw NetworkOnMainThreadException. Use AsyncTask or a separate thread to perform the Network Operation.

like image 196
Pragnani Avatar answered Sep 17 '26 06:09

Pragnani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!