我在Android中轉移JSON
時遇到問題。我在LogCat
中收到以下錯誤。JSON數據錯誤Android非法參數HttpClient
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":"[email protected] 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
這
,我試圖運行
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;
}
}
它要的東西與GET
調用Android的代碼。我不知道究竟是什麼,但我所有的System.out
打印到該程序的該部分...
在此先感謝您的幫助!
我希望周圍的教程和例子會提到這一點。沒有一個我讀過的例子是在做任何與網絡相關的事情時使用的「AsyncTask」。謝謝。 – nathansizemore 2013-04-29 17:05:34