1

私は、postメソッドを使用して2つのxmlをパラメーターとしてサーバーに送信する必要があるAndroidプロジェクトに取り組んでいます(つまり、フォームとして送信したい)。次のコードを使用してデータを送信しようとしましたが、機能しません。リモートデータベースにデータがありません。

private void postFormData(List<DataItem> ti,String ex,String getExpensesXml)
{    
//Create a new Http Client 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
try
{ 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("sync","true"));
    nameValuePairs.add(new BasicNameValuePair("tt",ti));
    nameValuePairs.add(new BasicNameValuePair("te",ex)); 

    UrlEncodedFormEntity form; 
    form = new UrlEncodedFormEntity(nameValuePairs,"UTF-8");  
    httppost.setEntity(form); 

    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity();  
    String line = EntityUtils.toString(entity);    
    System.out.println(line);

} catch (ClientProtocolException e) {
       e.printStackTrace();
    } catch (IOException e) {
       e.printStackTrace();
    } 
}

何が問題なのかわかりませんでした。誰かが問題を見つけて私に解決策を提案することができれば素晴らしいでしょう。

もう2つ質問がありますか?正しいコードを試していますか?フォームを介してxmlデータをサーバーに送信する他の方法はありますか?

前もって感謝します

4

2 に答える 2

0

同じ問題に直面しました。しかし、ついにサーバーにデータを投稿することができました。コードは、にエンコードしてStringからBase64に渡す必要があることを除けば、絶対にあるべき姿ですNameValuePairStringサーバー側では、からバックをデコードする必要がありますBase64。それが動作します。必要に応じてコードを共有できます。

于 2012-12-01T04:40:59.240 に答える
0

最後に私は解決策を見つけました、私は次のコードを使用してデータをfromからserverに送信しました

URL siteUrl;
    try {
        siteUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) siteUrl
                .openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        String content1 = ""; 

        Set getkey = param.keySet();
        Iterator keyIter = getkey.iterator();
        String content = "";
        for (int i = 0; keyIter.hasNext(); i++) {
            Object key = keyIter.next();
            if (i != 0) {
                content += "&";
            }
            content += key + "=" + param.get(key);
            System.out.println("Content" + content);
        }

        System.out.println(content);
        out.writeBytes(content.trim());
        out.flush();
        out.close();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        String line = "";
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();


    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    db.close();
}

私のような人には役立つかもしれません。

于 2012-08-24T13:41:55.333 に答える