我如何将字符串数据 ( JSONObject.toString()
) 发送到 url。我想在 util 类中编写一个静态方法来执行此操作。我希望方法签名如下
public static String postData (String url, String postData) throws SomeCustomException
字符串url的格式应该是什么
返回字符串是来自服务器的响应,作为 json 数据的字符串表示。
编辑
当前连接工具
package my.package;
import my.package.exceptions.CustomException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class ConnectionUtil {
public static String postData(String url, String postData)
throws CustomException {
// Create a new HttpClient and Post Header
InputStream is = null;
StringBuilder sb = null;
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost();
httppost.setHeader("host", url);
Log.v("ConnectionUtil", "Opening POST connection to URI = " + httppost.getURI() + " url = " + URLDecoder.decode(url));
try {
httppost.setEntity(new StringEntity(postData));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
e.printStackTrace();
throw new CustomException("Could not establish network connection");
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
throw new CustomException("Error parsing the response");
}
Log.v("ConnectionUtil", "Sent: "+postData);
Log.v("ConnectionUtil", "Got result "+result);
return result;
}
}
日志输出
10-16 11:27:27.287:E/log_tag(4935):http 连接出错 java.lang.NullPointerException 10-16 11:27:27.287:W/System.err(4935):java.lang.NullPointerException 10- 16 11:27:27.287: W/System.err(4935): 在 org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:496) 10-16 11:27:27.307: W/System。错误(4935):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)10-16 11:27:27.327:W/System.err(4935):在 org.apache.http .impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 10-16 11:27:27.327: W/System.err(4935): 在 in.gharpay.zap.integration.ConnectionUtil.postData(ConnectionUtil.java: 92) 10-16 11:27:27.327: W/System.err(4935): 在 in.gharpay.zap.integration.ZapTransaction$1.doInBackground(ZapTransaction.java:54) 10-16 11:27:27.327: W /System.err(4935):在 in.gharpay.zap.integration.ZapTransaction$1.doInBackground(ZapTransaction.java:1) 10-16 11:27:27.327: W/System.err(4935): 在 android.os.AsyncTask$2.call(AsyncTask. java:185) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent .ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 10-16 11:27:27.327: W/System.err(4935): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10- 16 11:27:27.327: W/System.err(4935): 在 java.lang.Thread.run(Thread.java:1019) 10-16 11:27:27.327: V/log_tag(4935): 无法建立网络连接ZapTransaction$1.doInBackground(ZapTransaction.java:1) 10-16 11:27:27.327: W/System.err(4935): 在 android.os.AsyncTask$2.call(AsyncTask.java:185) 10-16 11: 27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935):在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1088) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/ System.err(4935): at java.lang.Thread.run(Thread.java:1019) 10-16 11:27:27.327: V/log_tag(4935): 无法建立网络连接ZapTransaction$1.doInBackground(ZapTransaction.java:1) 10-16 11:27:27.327: W/System.err(4935): 在 android.os.AsyncTask$2.call(AsyncTask.java:185) 10-16 11: 27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935):在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java: 1088) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/ System.err(4935): at java.lang.Thread.run(Thread.java:1019) 10-16 11:27:27.327: V/log_tag(4935): 无法建立网络连接错误(4935):在 android.os.AsyncTask$2.call(AsyncTask.java:185)10-16 11:27:27.327:W/System.err(4935):在 java.util.concurrent.FutureTask$Sync。 innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27: 27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 10-16 11:27:27.327: W/System.err(4935): 在 java.util .concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/System.err(4935): at java.lang.Thread.run(Thread.java:1019) 10- 16 11:27:27.327: V/log_tag(4935): 无法建立网络连接错误(4935):在 android.os.AsyncTask$2.call(AsyncTask.java:185)10-16 11:27:27.327:W/System.err(4935):在 java.util.concurrent.FutureTask$Sync。 innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27: 27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 10-16 11:27:27.327: W/System.err(4935): 在 java.util .concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/System.err(4935): at java.lang.Thread.run(Thread.java:1019) 10- 16 11:27:27.327: V/log_tag(4935): 无法建立网络连接innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27: 27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 10-16 11:27:27.327: W/System.err(4935): 在 java.util .concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/System.err(4935): at java.lang.Thread.run(Thread.java:1019) 10- 16 11:27:27.327: V/log_tag(4935): 无法建立网络连接innerRun(FutureTask.java:306) 10-16 11:27:27.327: W/System.err(4935): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 10-16 11:27: 27.327: W/System.err(4935): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 10-16 11:27:27.327: W/System.err(4935): 在 java.util .concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/System.err(4935): at java.lang.Thread.run(Thread.java:1019) 10- 16 11:27:27.327: V/log_tag(4935): 无法建立网络连接concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/System.err(4935): 在 java.lang.Thread.run(Thread.java:1019) 10-16 11:27:27.327: V/log_tag(4935): 无法建立网络连接concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 10-16 11:27:27.327: W/System.err(4935): 在 java.lang.Thread.run(Thread.java:1019) 10-16 11:27:27.327: V/log_tag(4935): 无法建立网络连接