1
我正在工作的Android 1.6應用程序不工作,我正在聯繫RESTful服務器來下載JSON對象以將數據導入到應用程序。
清單文件有用戶權限對於互聯網和寫入外部存儲。
雖然我不確定我的權限是否正常。
需要發生的事情是PUT請求需要通過對象控件的JSON字符串轉到服務器。服務器更新控制對象並將其傳回。
我需要從控制對象中取出一個字段並將其保存到SD卡上的文件中。我做了什麼錯,或者我在哪裏可以找到更好的辦法?
網絡代碼是從Java應用程序中移植出來的,我認爲它在線路InputStream in = conn.getInputStream();
處死去。Android HttpURLConnect輸入流和文件寫入
Gson gson = new Gson();
try{
String uriAddress = "serveraddress";
URI uri = new URI(uriAddress);
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream out = conn.getOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(out);
String putRequest = gson.toJson(control);
objOut.writeObject(putRequest);
out.flush();
int x = conn.getResponseCode();
out.close();
InputStream in = conn.getInputStream();
ObjectInputStream objIn = new ObjectInputStream(in);
String jsonString = (String) objIn.readObject();
control = gson.fromJson(jsonString, WordToPictureMatchingDataTransferControl.class);
}catch(Exception e){
Log.e("Net", e.getMessage());
}
try {
FileOutputStream fOut = openFileOutput("/sdcard/CAT/wtpm/wtpm.dat", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//osw.write(gson.toJson(control.getWtpmdtArray()));
osw.write("It Worked");
osw.close();
fOut.close();
} catch (Exception e){
Log.e("File", e.getMessage());
}
下面是從執行棧跟蹤
03-16 23:01:15.105: ERROR/Net(23700): six8a.rdg.ac.uk/192.100.154.8:8080 - Connection refused
03-16 23:01:15.105: ERROR/Net(23700): six8a.rdg.ac.uk/192.100.154.8:8080 - Connection refused
03-16 23:01:15.105: WARN/System.err(23700): java.net.ConnectException: six8a.rdg.ac.uk/192.100.154.8:8080 - Connection refused
03-16 23:01:15.115: WARN/System.err(23700): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:254)
03-16 23:01:15.115: WARN/System.err(23700): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
03-16 23:01:15.115: WARN/System.err(23700): at java.net.Socket.connect(Socket.java:1055)
03-16 23:01:15.115: WARN/System.err(23700): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
03-16 23:01:15.115: WARN/System.err(23700): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
03-16 23:01:15.115: WARN/System.err(23700): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
03-16 23:01:15.115: WARN/System.err(23700): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
03-16 23:01:15.126: WARN/System.err(23700): at uk.ac.reading.sse.mz014227.cat.CAT.updateExercises(CAT.java:123)
03-16 23:01:15.126: WARN/System.err(23700): at uk.ac.reading.sse.mz014227.cat.CAT.onOptionsItemSelected(CAT.java:92)
03-16 23:01:15.126: WARN/System.err(23700): at android.app.Activity.onMenuItemSelected(Activity.java:2202)
03-16 23:01:15.126: WARN/System.err(23700): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:739)
03-16 23:01:15.126: WARN/System.err(23700): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
03-16 23:01:15.126: WARN/System.err(23700): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
03-16 23:01:15.126: WARN/System.err(23700): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:545)
03-16 23:01:15.126: WARN/System.err(23700): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
03-16 23:01:15.126: WARN/System.err(23700): at android.view.View$PerformClick.run(View.java:8817)
03-16 23:01:15.126: WARN/System.err(23700): at android.os.Handler.handleCallback(Handler.java:587)
03-16 23:01:15.126: WARN/System.err(23700): at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 23:01:15.126: WARN/System.err(23700): at android.os.Looper.loop(Looper.java:144)
03-16 23:01:15.126: WARN/System.err(23700): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-16 23:01:15.126: WARN/System.err(23700): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 23:01:15.135: WARN/System.err(23700): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 23:01:15.135: WARN/System.err(23700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 23:01:15.135: WARN/System.err(23700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 23:01:15.135: WARN/System.err(23700): at dalvik.system.NativeStart.main(Native Method)
03-16 23:01:15.135: ERROR/File(23700): File /sdcard/CAT/wtpm/wtpm.dat contains a path separator
03-16 23:01:15.135: ERROR/File(23700): File /sdcard/CAT/wtpm/wtpm.dat contains a path separator
03-16 23:01:15.135: WARN/System.err(23700): java.lang.IllegalArgumentException: File /sdcard/CAT/wtpm/wtpm.dat contains a path separator
03-16 23:01:15.135: WARN/System.err(23700): at android.app.ContextImpl.makeFilename(ContextImpl.java:1641)
03-16 23:01:15.135: WARN/System.err(23700): at android.app.ContextImpl.openFileOutput(ContextImpl.java:415)
03-16 23:01:15.135: WARN/System.err(23700): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
03-16 23:01:15.135: WARN/System.err(23700): at uk.ac.reading.sse.mz014227.cat.CAT.updateExercises(CAT.java:147)
03-16 23:01:15.146: WARN/System.err(23700): at uk.ac.reading.sse.mz014227.cat.CAT.onOptionsItemSelected(CAT.java:92)
03-16 23:01:15.146: WARN/System.err(23700): at android.app.Activity.onMenuItemSelected(Activity.java:2202)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:739)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:545)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
03-16 23:01:15.146: WARN/System.err(23700): at android.view.View$PerformClick.run(View.java:8817)
03-16 23:01:15.146: WARN/System.err(23700): at android.os.Handler.handleCallback(Handler.java:587)
03-16 23:01:15.146: WARN/System.err(23700): at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 23:01:15.146: WARN/System.err(23700): at android.os.Looper.loop(Looper.java:144)
03-16 23:01:15.146: WARN/System.err(23700): at android.app.ActivityThread.main(ActivityThread.java:4937)
03-16 23:01:15.146: WARN/System.err(23700): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 23:01:15.146: WARN/System.err(23700): at java.lang.reflect.Method.invoke(Method.java:521)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-16 23:01:15.146: WARN/System.err(23700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-16 23:01:15.146: WARN/System.err(23700): at dalvik.system.NativeStart.main(Native Method)
校正碼是
Gson gson = new Gson();
String jsonString = "";
String encoding = "UTF-8";
try{
String uriAddress = "address";
URI uri = new URI(uriAddress);
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
String putRequest = gson.toJson(control);
Log.d("Gson", gson.toJson(control));
out.write(putRequest);
out.flush();
int x = conn.getResponseCode();
Log.d("Repsonse Code", "" + x);
out.close();
InputStream in = conn.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer, encoding);
jsonString = writer.toString();
}catch(Exception e){
Log.e("Net", e.getMessage());
Log.e("Net", e.getLocalizedMessage());
e.printStackTrace();
}
Log.e("Gson2", jsonString);
control = gson.fromJson(jsonString, WordToPictureMatchingDataTransferControl.class);
File file = new File("/sdcard/CAT/wtpm.dat");
FileOutputStream binOutFile;
ObjectOutputStream objOut;
// initialise FileOutputStream
try {
binOutFile = new FileOutputStream(file);
objOut = new ObjectOutputStream(binOutFile);
objOut.writeObject(control.getWtpmdtArray());
objOut.close();
binOutFile.close();
} catch (Exception e) {
// if exception warn and return
Log.e("File", e.getLocalizedMessage());
Log.e("File", e.getMessage());
e.printStackTrace();
}
}
當你說 「我認爲這是垂死在該行的InputStream在= conn.getInputStream();」你什麼意思?應用程序部隊是否關閉?或者只是不按預期行事?如果是強制關閉,如果您發佈logcat的輸出,我們將更容易提供幫助。 – FoamyGuy 2011-03-16 21:08:53
logcat不發佈任何內容。我不得不逐行強制進行佈局更改,以顯示它有多遠。我可以在該行之前進行佈局更改,如果在該行後面進行佈局更改,則不會更改佈局。所以我認爲它正在拋出一個異常,而不是logcat,但我找不出原因。 – 2011-03-16 21:19:21
似乎glassfish RESTful服務器拒絕連接 – 2011-03-16 23:11:48