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です
変換したいクラスはこれです(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;
}
}
私への愛はありますか?