1

postメソッドを使用してAndroidデバイスからasp.netページに簡単なデータを送信したいのですが、AndroidからWebページをリクエストする方法がわかりませんでした。

aspページは良好で、エラーなしでデータに応答できます。しかし、Androidアプリケーションの問題...

今私はこのコードを使用していますが、機能しませんでした

public void postData() throws ClientProtocolException, IOException, Exception {       
    String key = "https://www.itrack.somee.com/post.aspx?id=10&long=123&lat=123&alt=123";
    //URI  uri=new URI(key);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(key);

    Toast.makeText(this, "here", Toast.LENGTH_LONG).show();
    HttpResponse response = httpclient.execute(httppost);
    Toast.makeText(this,"mm"+response.toString(), Toast.LENGTH_LONG).show();
}

助けて.....!!

4

3 に答える 3

2

ArrayListメソッドを使用して、のを作成し、nameValuePairsそれらをHttpPostにアタッチできますsetEntity

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Var1",Var1_value));
nameValuePairs.add(new BasicNameValuePair("Var2",Var2_value));
//...ect
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("URL_HERE");       
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
于 2012-11-06T21:25:49.363 に答える
0

MrZanderの答えが飛び出したので、私はこれを書いていました... @MrZanderの答えに基づいて、ここに例があります。これが機能する場合は、彼の回答を承認済みとしてマークしてください。

public void postData() throws ClientProtocolException, IOException, Exception {       
    String key = "https://www.itrack.somee.com/post.aspx";
    //URI  uri=new URI(key);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(key);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
    nameValuePairs.add(new BasicNameValuePair("id", "10"));
    nameValuePairs.add(new BasicNameValuePair("long", "123"));
    nameValuePairs.add(new BasicNameValuePair("lat", "123"));
    nameValuePairs.add(new BasicNameValuePair("alt", "123"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    Toast.makeText(this, "here", Toast.LENGTH_LONG).show();
    HttpResponse response = httpclient.execute(httppost);
    Toast.makeText(this,"mm"+response.toString(), Toast.LENGTH_LONG).show();
}

またはさらに良いことに、私はあなたが多分このようなものを探していると信じていますか?:

public void postData(int id, double lat, double lng, double alt) throws ClientProtocolException, IOException, Exception {       
    String key = "https://www.itrack.somee.com/post.aspx";
    //URI  uri=new URI(key);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(key);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
    nameValuePairs.add(new BasicNameValuePair("id", "" + id));
    nameValuePairs.add(new BasicNameValuePair("long", String.valueOf(lat));
    nameValuePairs.add(new BasicNameValuePair("lat", String.valueOf(lng));
    nameValuePairs.add(new BasicNameValuePair("alt", String.valueOf(alt)));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    Toast.makeText(this, "here", Toast.LENGTH_LONG).show();
    HttpResponse response = httpclient.execute(httppost);
    Toast.makeText(this,"mm"+response.toString(), Toast.LENGTH_LONG).show();
}
于 2012-11-06T21:31:19.473 に答える
-1

私のWebサービスの理解から、postメソッドでは、URLパラメーターをURLの一部としてではなく、本文で送信する必要があります。

ここで、key変数は、代わりに本文を設定する必要がある場合に、パラメーターとして投稿データを含むURLを特徴としています。

get requestsURLパラメータ post requestsを使用するヘッダーと本文を使用する

于 2012-11-06T21:17:34.970 に答える