在Android上使用POST发布数据

时间:2012-05-21 08:30:26

标签: android image http-post

我一直在搜索两周,如何使用Android上的帖子发布数据,我到处寻找,尝试了所有解决方案,但它仍然无效。 现在,我只需要发布一个字符串链,后来我将不得不用一些字符串链发布图片。我的实际代码是:

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 个答案:

答案 0 :(得分: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();
        }

答案 1 :(得分: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";
    }

}

答案 2 :(得分:0)

之后添加
HttpResponse response = client.execute(post, new BasicHttpContext());

- &GT;

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

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

答案 3 :(得分: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());
    }
}