Java 将原始格式的数据发布到PHP文件并获得响应

Java 将原始格式的数据发布到PHP文件并获得响应,java,php,android,post,Java,Php,Android,Post,我正在制作一个android应用程序,在这个应用程序中,我需要使用post数据将从数据服务器收集的一些数据发送到php文件,并从php文件中获取回显文本并显示出来。我有这种格式的post变量->“name=xyz&home=xyz”等等。我使用下面的类来post,但是服务器上的php文件没有获得post变量。有人能告诉我什么地方不对,或者我想做的事情有什么其他的方法吗 package xxx.xxx.xxx; import java.io.BufferedReader; import java

我正在制作一个android应用程序,在这个应用程序中,我需要使用post数据将从数据服务器收集的一些数据发送到php文件,并从php文件中获取回显文本并显示出来。我有这种格式的post变量->“name=xyz&home=xyz”等等。我使用下面的类来post,但是服务器上的php文件没有获得post变量。有人能告诉我什么地方不对,或者我想做的事情有什么其他的方法吗

package xxx.xxx.xxx;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetUtil {
    public static String UrlToString(String targetURL, String urlParameters)
      {
        URL url;
        HttpURLConnection connection = null;  
        try {
          //Create connection
          url = new URL(targetURL);
          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", 
               "application/x-www-form-urlencoded");

          connection.setRequestProperty("Content-Length", "" + 
                   Integer.toString(urlParameters.getBytes().length));
          connection.setRequestProperty("Content-Language", "en-US");  

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);

          //Send request
          DataOutputStream wr = new DataOutputStream (
                      connection.getOutputStream ());
          wr.write(urlParameters.getBytes("UTF-8"));
          wr.flush ();
          //Get Response    
          InputStream is = connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;
          StringBuffer response = new StringBuffer(); 
          while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
          }
          rd.close();
          return response.toString();

        } catch (Exception e) {

          e.printStackTrace();
          return null;

        } finally {

          if(connection != null) {
            connection.disconnect(); 
          }
        }
      }

}

我从php文件中得到了一个响应,但是php文件没有得到post数据。

对于初学者来说,这个示例看起来不错。只有以下几点出现:

connection.setRequestProperty("Content-Length", "" + 
    Integer.toString(urlParameters.getBytes().length));

首先,使用平台默认编码将字符转换为字节。生成的长度不一定与使用UTF-8编码将字符转换为字节时的长度相同,就像编写请求正文时那样。因此,
内容长度
标题有可能偏离实际内容长度。要解决这个问题,您应该在两个调用上使用相同的字符集

但是我相信PHP在解析请求体时并没有那么严格,所以在PHP端什么也得不到。可能
urlParameters
的格式不正确。它们真的是URL编码的吗

不管怎样,你有没有试过Android的内置HttpClient API?它应该简单如下:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(targetURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "xyz"));
params.add(new BasicNameValuePair("home", "xyz"));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
InputStream input = response.getEntity().getContent();
// ...
HttpClient=newdefaulthttpclient();
HttpPost=新的HttpPost(targetURL);
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“名称”、“xyz”);
参数添加(新的BasicNameValuePair(“主”、“xyz”);
post.setEntity(新的UrlEncodedFormEntity(params));
HttpResponse response=client.execute(post);
InputStream输入=response.getEntity().getContent();
// ...

如果这不起作用,那么PHP端可能会出现错误。

谢谢您的回复。我可以看出上面的代码有什么问题。关于httpclientapi,我以get数据格式收集数据,如“name=xyz&phone=abc&number=123”,是否有方法将该格式与httpclientapi一起使用,或将该格式更改为namevalue对。上面的HttpClient API将发布数据,但是如何从php文件中获得字符串形式的回复呢?再次感谢您:请以不同的方式收集它们。使用
Map
是最简单的选择。将其作为方法参数传递,然后在其上循环,每次都向列表中添加一个新的
BasicNameValuePair
。至于阅读响应正文,请阅读我在代码示例中为您准备的
输入。如果我遇到问题,我会尝试回答:D@BalusC我得到了其他的一切,但是我如何制作一个映射并将其传递给新方法呢?我对android和java完全陌生,如果可能的话,请给我一个示例代码。谢谢:这行不行?我不明白“我还有其他东西”那部分。至于地图,只要看地图或者问一个新问题就行了。
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(targetURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", "xyz"));
params.add(new BasicNameValuePair("home", "xyz"));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(post);
InputStream input = response.getEntity().getContent();
// ...