2012-04-03 5 views
0

NameValuePairを使用せずにデータを投稿するにはどうすればよいですか?NameValuePairを使用しないAndroid POST

私が使用することになり、JSの文字列 を投稿したい:

var separator = String.fromCharCode(0xef, 0xbf, 0xbf); 
var postUrl = "PostUrl"; 
var postData = "3" + separator + "0" + separator + "13" 
network.setUnicode(true); 
this.reply = network.post(postUrl, postData); 

答えて

2

Androidに付属のHTTP Clientをご覧ください。あなたの場合は、StringEntityをPOST要求のペイロードとして使用することになります。

public void postString(URI uri, String yourDataString) throws IOException { 
    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(uri); 
    post.setEntity(new StringEntity(yourDataString)); 

    client.execute(post); 
} 

明らかに、エラー条件などをより慎重にチェックしたいと思うでしょう。

+0

これは答えです:) –

0

は、以下のようなURLに何かを構築します。それはうまくいくはずです。私はアンドロイド/ Javaでデータをポストするために、このメソッドを使用し

http://www.site.com?postData=3&separator=0&anotherParam=value; 
0
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("userid","123")); 
       nameValuePairs.add(new BasicNameValuePair("date","2012")); 
       nameValuePairs.add(new BasicNameValuePair("op","update")); 
      try { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://www.myurl.com/action/put/newrecord"); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 
      } 

。しかし、私はこれが正しい方法だと思います!

+0

ええと...私はこの方法を知っています。私はそれがペアなしでなければならないと言った:) –

+0

mmmmmあなたはあまりにも認証することができますので、私はこの方法を知っている:)しかし、あなたのパラメータで 'URL'を作ってみてください。 –

1
URL url; 
    URLConnection urlConnection; 
    DataOutputStream outStream; 
    DataInputStream inStream; 

    // Build request body 
    String body = "3" + separator + "0" + separator + "13" 

    // Create connection 
    url = new URL("http://www.site.com"); 
    urlConnection = url.openConnection(); 
    ((HttpURLConnection)urlConnection).setRequestMethod("POST"); 
    urlConnection.setDoInput(true); 
    urlConnection.setDoOutput(true); 
    urlConnection.setUseCaches(false); 
    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    urlConnection.setRequestProperty("Content-Length", ""+ body.length()); 

    // Create I/O streams 
    outStream = new DataOutputStream(urlConnection.getOutputStream()); 
    inStream = new DataInputStream(urlConnection.getInputStream()); 

    // Send request 
    outStream.writeBytes(body); 
    outStream.flush(); 
    outStream.close(); 

    // Get Response 
    String buffer; 
    while((buffer = inStream.readLine()) != null) { 
     System.out.println(buffer); 
    } 

    // Close I/O streams 
    inStream.close(); 
    outStream.close(); 
+1

うーん、私はまだこのURLConnection、DataStreamsを使用していないが、動作するようです。ありがとう –

+0

これに何が痛いですか? –

関連する問題