JSON对象未发布

时间:2017-03-13 19:49:12

标签: php android json gson

我正在尝试使用以下代码发布JSON对象:

public static final String HttpPostExec (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("POST"); //We be doing a POST
        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();

        System.out.println("hey");


        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;

}

以下是我的HTTP处理程序中的代码:

    private String ID, Name, Price, Comment, LastModified;

public Beer() {
}

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 + '\'' +
            '}';
}

当我执行'postABeer'时,会显示以下错误消息:

  

java.io.FileNotFoundException:wirelessward.net/beer/gateway.php/beer/973

这是我的啤酒课:

var text = document.getElementById('text');
var greeting = [
    'text that should be a link'
];
text.innerHTML = '<i>▮</i>';

(function greet() {
    if (greeting.length > 0 && greeting.length < 3) {
        text.insertBefore(document.createElement(' '), text.lastChild);
    }

    var line = greeting.shift();
    if (!line) {
        return;
    }

    line = line.split('');
    (function type() {
        var character = line.shift();
        if (!character) {
            return setTimeout(greet, 100);
        }

        text.insertBefore(document.createElement(character), text.lastChild);
        setTimeout(type, 20);
    }());
}());

有谁知道为什么会这样?

由于

1 个答案:

答案 0 :(得分:0)

关于Volley Request的研究。

final TextView mTextView = (TextView) findViewById(R.id.text);
...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);