无法在Android

时间:2017-03-13 21:37:20

标签: php android json

我正在尝试创建一个JSON对象,然后发布它。

以下是我用来发布啤酒的代码。

当我尝试检索此啤酒时,我收到 FileNoteFoundException ,有人可以告诉我为什么该对象未成功发布吗?

我已经发布了我认为的相关代码。

public void postABeer(final int beerId, final Handler h) {

        new Thread (myThreadGroup, new Runnable () {

            @Override
            public void run() {

                BeerAdapter ba = new BeerAdapter();
                Beer newBeer = new Beer();
                newBeer = ba.createBeer("{\"ID\":\"973\", \"Name\":\"Beer\", \"Price\":\"4.99\", \"Comment\":\"bla\", \"LastModified\":\"196\"}");

                //Do background stuff here.
                String s = HttpHandler.HttpPostExec(MainActivity.baseURI
                        + "beer/" + beerId, newBeer.toString());

                //Start of artificial delay.Comment out if not wanted.
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //End of artificial delay.

                Message m = new Message();
                m.obj = s;
                h.sendMessage(m);

            }//end run()

        }).start();

    }//end postABeer()

这是我的BeerAdapter:

public class BeerAdapter {

public static final Beer convertToBeer (String beerString) {
    Gson g = new Gson();
    Beer b = g.fromJson(beerString, Beer.class);
    return b;
}

public static final Beer createBeer (String beerString) {
    Gson g = new Gson();
    Beer b = g.fromJson(beerString, Beer.class);
    return b;
}

}

这是我的啤酒类:

    public Beer(String ID, String name, String price, String comment, String lastModified) {
    this.ID = ID;
    Name = name;
    Price = price;
    Comment = comment;
    LastModified = lastModified;
}

@Override
public String toString() {
    return "Beer{" +
            "ID='" + ID + '\'' +
            ", Name='" + Name + '\'' +
            ", Price='" + Price + '\'' +
            ", Comment='" + Comment + '\'' +
            ", LastModified='" + LastModified + '\'' +
            '}';
}

这是Put声明:

public static final String HttpPutExec (String uri, String payload) {

    String s = "no response";
    HttpURLConnection conn = null;
    int http_status = 0;

    try {
        byte[] payloadBytes = payload.getBytes();
        URL url = new URL(uri);

        conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("PUT"); //We be doing a PUT
        conn.setRequestProperty("Content-Type", "application/json"); //We are sending JSON
        conn.setFixedLengthStreamingMode(payloadBytes.length);

        OutputStream out = conn.getOutputStream();

        out.write(payloadBytes);

        InputStream in = conn.getInputStream();
        http_status = conn.getResponseCode();

        if (http_status == 200) {
            s = streamToString(in);
        } else {
            s = "bad response";
        }
    } catch (MalformedURLException m) {
        s = "malformed URL exception";
    } catch (IOException e) {
        s = e.toString();
    } finally {
        conn.disconnect();
    }
    return s;

}

错误:

  

03/13 21:52:08:启动MainActivity   W / System:ClassLoader引用未知路径:/data/data/ie.ucc.bis.is4448.is4448servicetestingdemo/lib   E / EGL_emulation:tid 4483:eglSurfaceAttrib(1146):错误0x3009(EGL_BAD_MATCH)   W / OpenGLRenderer:无法在表面0x8b373540上设置EGL_SWAP_BEHAVIOR,错误= EGL_BAD_MATCH   热交换更改,活动重新启动   E / EGL_emulation:tid 4483:eglSurfaceAttrib(1146):错误0x3009(EGL_BAD_MATCH)   W / OpenGLRenderer:无法在表面0x8b373640上设置EGL_SWAP_BEHAVIOR,错误= EGL_BAD_MATCH

1 个答案:

答案 0 :(得分:0)

只需使用retrofit

然后你可以发送像

这样的POST请求
@POST("users/new")
Call<User> createUser(@Body User user);

@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);