在Android上使用POST发布数据
我已经搜索了两个星期,如何在Android上使用post发布数据,我到处寻找,尝试了各种解决方案,但仍然不起作用。 现在,我只需要发布一个字符串链,稍后我将不得不发布一些带有字符串链的图片。我的实际代码是:在Android上使用POST发布数据,android,image,http-post,Android,Image,Http Post,我已经搜索了两个星期,如何在Android上使用post发布数据,我到处寻找,尝试了各种解决方案,但仍然不起作用。 现在,我只需要发布一个字符串链,稍后我将不得不发布一些带有字符串链的图片。我的实际代码是: public String Publication() { try { Intent intentget = getIntent(); String titre = intentget.getExtras().getString("titre"
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";
}
}
公共字符串发布()
{
尝试
{
Intent intentget=getIntent();
字符串titre=intentget.getExtras().getString(“titre”);
字符串poster=titre+“=”+getTableau(_-tlab,iNbLignes,iNbCol);
Log.d(“回复”、“gettableau”);
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://tweet.envrac.ch/Publication.php");
Log.d(“回复”、“url”);
列表对=新的ArrayList(1);
添加(新的BasicNameValuePair(“LabColler”,海报));
Log.d(“回复”、“配对”);
setEntity(新的UrlEncodedFormEntity(对));
Log.d(“回复”、“编码”);
//臭虫
HttpResponse response=client.execute(post,new BasicHttpContext());
日志d(“回复”、“发布”);
返回“response=”+响应;
}
捕获(例外e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
返回“erreur”;
}
}
希望您能帮助我。也许这段代码可以帮助您
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();
}
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“firstname”,str_firstname));
添加(新的BasicNameValuePair(“middlename”,str_middlename));
添加(新的BasicNameValuePair(“lastname”,str_lastname));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
URI=URIUtils.createURI(“http”、“www.abc.com”、-1、“dosomething.php”,
URLEncodedUtils.format(nameValuePairs,“UTF-8”),null;
Log.i(“URI:,URI.toString());
HttpPost HttpPost=新的HttpPost(uri);
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
}捕获(例外e){
Toast.makeText(getApplicationContext(),“Error”,Toast.LENGTH_LONG.show();
}
我稍微修改了你的代码,试试这个
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";
}
}
公共字符串发布()
{
尝试
{
Intent intentget=getIntent();
字符串titre=intentget.getExtras().getString(“titre”);
字符串poster=titre+“=”+getTableau(_-tlab,iNbLignes,iNbCol);
Log.d(“回复”、“gettableau”);
HttpClient=new DefaultHttpClient();
ResponseHandler res=new BasicResponseHandler();//新添加的行
HttpPost=新的HttpPost(“http://tweet.envrac.ch/Publication.php");
Log.d(“回复”、“url”);
列表对=新的ArrayList(1);
添加(新的BasicNameValuePair(“LabColler”,海报));
Log.d(“回复”、“配对”);
setEntity(新的UrlEncodedFormEntity(对));
Log.d(“回复”、“编码”);
//臭虫
HttpResponse response=client.execute(post,res);//已更改行
日志d(“回复”、“发布”);
返回“response=”+响应;
}
捕获(例外e)
{
//TODO自动生成的捕捉块
e、 printStackTrace();
返回“erreur”;
}
}
将此添加到后面
HttpResponse response = client.execute(post, new BasicHttpContext());
->
此外,检查清单中的互联网权限和url中不允许的符号 这是我用来将值发布到服务器的代码。它工作正常,如果可能的话,请按照您的方式修改此代码
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&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());
}
}
我可以知道,一旦这个密码生效,确认。谢谢……:)你在清单中添加了所需的权限了吗?看起来它没有执行,行“HttpResponse response=client.execute(post,new BasicHttpContext());”。那么日志中有什么有用的东西吗。可能会让其他人知道出了什么问题。恐怕没有什么有用的
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&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());
}
}