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", "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;
}
}
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!