0

我已经搜索了两个星期,如何在 Android 上使用 post 发布数据,我到处寻找,尝试了所有解决方案,但它仍然不起作用。现在,我只需要发布一个字符串链,稍后我将不得不发布带有一些字符串链的图片。我的实际代码是:

public String Publication()
{
    try
    {
        Intent intentget = getIntent();
        String titre = intentget.getExtras().getString("titre");
        String poster = titre +"="+getTableau(_tlab, iNbLignes, iNbCol);
        Log.d("reponse","gettableau");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://tweet.envrac.ch/Publication.php");
        Log.d("reponse","url");
        List<NameValuePair> pairs = new ArrayList<NameValuePair>(1);
        pairs.add(new BasicNameValuePair("LabColler", poster));
        Log.d("reponse","pairs");
        post.setEntity(new UrlEncodedFormEntity(pairs));
        Log.d("reponse","encode");
        //bug ici
        HttpResponse response = client.execute(post, new BasicHttpContext());
        Log.d("reponse","post");
        return "reponse =  "+response;
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "erreur";
    }

}

希望您能够帮助我。

4

4 回答 4

0

也许这段代码可以帮助你。

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("firstname", str_firstName));
        nameValuePairs.add(new BasicNameValuePair("middlename", str_middleName));
        nameValuePairs.add(new BasicNameValuePair("lastname", str_lastName));

        try{
            HttpClient httpClient = new DefaultHttpClient();
            URI uri = URIUtils.createURI("http","www.abc.com", -1,                              "dosomething.php",
                    URLEncodedUtils.format(nameValuePairs, "UTF-8"), null);
            Log.i("URI : " , uri.toString());
            HttpPost httpPost = new HttpPost(uri);
            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();
           }catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
        }
于 2012-05-21T08:37:17.353 回答
0

我稍微修改了你的代码,试试这个

public String Publication()
{
    try
    {
        Intent intentget = getIntent();
        String titre = intentget.getExtras().getString("titre");
        String poster = titre +"="+getTableau(_tlab, iNbLignes, iNbCol);
        Log.d("reponse","gettableau");
        HttpClient client = new DefaultHttpClient();
        ResponseHandler<String> res = new BasicResponseHandler(); //newly added line
        HttpPost post = new HttpPost("http://tweet.envrac.ch/Publication.php");
        Log.d("reponse","url");
        List<NameValuePair> pairs = new ArrayList<NameValuePair>(1);
        pairs.add(new BasicNameValuePair("LabColler", poster));
        Log.d("reponse","pairs");
        post.setEntity(new UrlEncodedFormEntity(pairs));
        Log.d("reponse","encode");
        //bug ici
        HttpResponse response = client.execute(post, res); // Changed line
        Log.d("reponse","post");
        return "reponse =  "+response;
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "erreur";
    }

}
于 2012-05-21T08:37:35.497 回答
0

之后添加这个

HttpResponse response = client.execute(post, new BasicHttpContext());

->

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) 
{
    Log.e("HttpResponse", line);
}

此外,检查清单中的互联网许可和您的网址中不允许的符号;

于 2012-05-21T08:39:30.733 回答
0

这是我用来将值发布到服务器的代码,它工作正常,如果可能的话,意味着根据你的修改此代码,

public void executeMultipartPost() throws Exception {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bm.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(
                "http://10.0.2.2/cfc/iphoneWebservice.cfc?returnformat=json&amp;method=testUpload");
        ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
        // File file= new File("/mnt/sdcard/forest.png");
        // FileBody bin = new FileBody(file);
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("uploaded", bab);
        reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        System.out.println("Response: " + s);
    } catch (Exception e) {
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());
    }
}
于 2012-05-21T08:48:56.550 回答