我尝试制作一个支持 AsyncTask 的类,但我失败了。后来我制作了这个类的超类,并在我的主类中添加了这个:
//subclass of the superclass CustomHttpClient
CustomHttpClient2 cl= new CustomHttpClient2();
cl.execute();
response = cl.executeHttpPost("http://ironis.dyndns.org/aeglea/android/log.php", postParameters);
错误消息是 android.os.NetworkOnMainThreadException
我要转换的类是this(我在sdk 7中使用它,但我移动到14,现在它失败了)
public class CustomHttpClient2 (extends CustomHttpClient)<<I used it without this {
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;
/**
* Get our single instance of our HttpClient object.
*
* @return an HttpClient object with connection parameters set
*/
private static HttpClient getHttpClient() {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
}
return mHttpClient;
}
/**
* Performs an HTTP Post request to the specified url with the
* specified parameters.
*
* @param url The web address to post the request to
* @param postParameters The parameters to send via the request
* @return The result of the request
* @throws Exception
*/
public JSONObject executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
BufferedReader in = null;
HttpEntity he=null;
JSONObject jo=null;
try {
HttpClient client = getHttpClient();
HttpPost request = new HttpPost(url);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
//mine
he=response.getEntity();
jo=new JSONObject(EntityUtils.toString(he));
//end mine
return jo;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Performs an HTTP GET request to the specified url.
*
* @param url The web address to post the request to
* @return The result of the request
* @throws Exception
*/
public static String executeHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我使用 CustomHttpClient 的另一个类如下
public class CustomHttpClient extends AsyncTask<HttpClient,String,String> {
public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;
protected HttpClient doInBackground(String... params) {
if (mHttpClient == null) {
mHttpClient = new DefaultHttpClient();
final HttpParams params1 = mHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params1, HTTP_TIMEOUT);
HttpConnectionParams.setSoTimeout(params1, HTTP_TIMEOUT);
ConnManagerParams.setTimeout(params1, HTTP_TIMEOUT);
}
return mHttpClient;
}
@Override
protected String doInBackground(HttpClient... params) {
// TODO Auto-generated method stub
return null;
}
}
对我有爱吗?