2017-03-13 20 views
0

投稿はありません:JSONオブジェクトは、私は以下のコードを使用して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 + '\'' + 
      '}'; 
} 

誰でも知っていますか?

おかげ

答えて

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); 
関連する問題