Я пытаюсь опубликовать объект JSON с помощью кода ниже:
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()
}//end BackGroundBinder class
Ниже приведен код в моем HTTP-обработчике:
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;
}
Когда я выполняю postABeer, появляется следующее сообщение об ошибке:
java.io.FileNotFoundException: wirelessward.net/beer/gateway.php/beer/973
Вот мой класс пива также:
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 + '\'' +
'}';
}
Кто-нибудь знает, почему это может происходить?
Спасибо
Учиться о Запрос залпа.
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);
Других решений пока нет …