Post 到 PHP 脚本 API 22
Post to PHP script in API 22
在 API 22 中,一切似乎都已被弃用。我可以 post 连接到 API 22 以下的服务器,但是 API 22 以上是如何完成的?我找不到任何东西,因为它还是太新了。
这是我在 API 22
下面的工作
public static void sendData(final String name, final String from, final String message)
{
Thread thread = new Thread(new Runnable(){
@Override
public void run(){
String content = "";
//code to do the HTTP request
try
{
/* Sends data through a HTTP POST request */
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://mywebsite.com/contact.php");
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("cf_name", name));
params.add(new BasicNameValuePair("cf_email", from));
params.add(new BasicNameValuePair("cf_message", message));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* Reads the server response */
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = in.read()) != -1)
{
sb.append((char) chr);
}
content = sb.toString();
in.close();
/* If there is a response, display it */
if (!content.equals(""))
{
Log.i("HTTP Response", content);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
thread.start();
}
最后我尝试了很多解决方案,唯一有效的是来自 Fahim
的 answer
在 API 22 中,一切似乎都已被弃用。我可以 post 连接到 API 22 以下的服务器,但是 API 22 以上是如何完成的?我找不到任何东西,因为它还是太新了。 这是我在 API 22
下面的工作public static void sendData(final String name, final String from, final String message)
{
Thread thread = new Thread(new Runnable(){
@Override
public void run(){
String content = "";
//code to do the HTTP request
try
{
/* Sends data through a HTTP POST request */
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://mywebsite.com/contact.php");
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("cf_name", name));
params.add(new BasicNameValuePair("cf_email", from));
params.add(new BasicNameValuePair("cf_message", message));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* Reads the server response */
HttpResponse response = httpClient.execute(httpPost);
InputStream in = response.getEntity().getContent();
StringBuffer sb = new StringBuffer();
int chr;
while ((chr = in.read()) != -1)
{
sb.append((char) chr);
}
content = sb.toString();
in.close();
/* If there is a response, display it */
if (!content.equals(""))
{
Log.i("HTTP Response", content);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
thread.start();
}
最后我尝试了很多解决方案,唯一有效的是来自 Fahim
的 answer