开发者

Android HttpURLConnect input stream and file writing

开发者 https://www.devze.com 2023-02-17 19:31 出处:网络
I am working on an Android 1.6 application that isn\'t working and I am contacting a RESTful server to download a JSON object to import data to the application.

I am working on an Android 1.6 application that isn't working and I am contacting a RESTful server to download a JSON object to import data to the application.

The manifest file has usesPermissions for Internet and Write External Storage.

Though I am not sure that my permissions are working.

What needs to happen is a PUT request needs to go to the server with a JSON string of the object control. The server updates the control object and passes it back.

I need to take a field out of the control object and save that to a file on the sdcard. What have I done wrong or where can I find a better idea of what to do?

The network code is ported from a Java application where it works, I think it is dying at the line InputStream in = conn.getInputStream();.

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());
    }

Here are the stack traces from the execution

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.10开发者_运维技巧5: 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)

The corrected code is

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();
    }

}


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();
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号