我有一个HttpRequestActivity类,它从android应用程序调用服务器。我必须为这个类编写一个测试用例来检查收到的字符串是否是一个JSON字符串。我该怎么办呢。我试图实现这个方法
public class HttpRequestActivityTest extends
android.test.ActivityUnitTestCase<HttpRequestActivity> {
它给出了HttpRequestActivity不是活动的错误,因此不能成为ActivityUnitTestCase的正确参数。但我不能编写Java JUnit testCase计划,因为这个类导入了android import&#34; import android.os。的AsyncTask;&#34;
该方法应该是什么?
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.os.AsyncTask;
public class HttpRequestActivity {
public String url_str, response_str = "";
HttpClient client;
JSONObject json;
public HttpRequestActivity(String targetUrl, String proxyHost) {
if (!proxyHost.equals("")) {
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", "8080");
}
url_str = targetUrl;
}
public String getResponseString() throws InterruptedException,
ExecutionException, IOException {
ResponseString response = new ResponseString();
return response.execute(url_str).get();
}
public class ResponseString extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
client = new DefaultHttpClient();
// HttpPost post = new HttpPost(url_str);
HttpGet get = new HttpGet(url_str);
try {
HttpResponse response = client.execute(get);
HttpEntity e = response.getEntity();
response_str = EntityUtils.toString(e);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response_str;
}
}
}
答案 0 :(得分:0)
使用此:
package com.yourpackage;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.util.Log;
/**
* This class sends your data through GET and POST methods
*
* */
public class HttpRequest {
DefaultHttpClient httpClient;
HttpContext localContext;
private String ret;
HttpResponse response = null;
HttpPost httpPost = null;
HttpGet httpGet = null;
@SuppressWarnings("rawtypes")
Map.Entry me;
@SuppressWarnings("rawtypes")
Iterator i;
public HttpRequest() {
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 50000);
HttpConnectionParams.setSoTimeout(myParams, 50000);
httpClient = new DefaultHttpClient(myParams);
localContext = new BasicHttpContext();
}
public void clearCookies() {
httpClient.getCookieStore().clear();
}
public String sendGet(String url) {
httpGet = new HttpGet(url);
try {
response = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
try {
ret = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e("HttpRequest", "" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public String postData(String url, List<NameValuePair> nameValuePairs)
throws Exception {
// Getting the response handler for handling the post response
ResponseHandler<String> res = new BasicResponseHandler();
HttpPost postMethod = new HttpPost(url);
// Setting the data that is to be sent
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
String response = httpClient.execute(postMethod, res);
return response;
}
}
现在从您的活动中调用此功能:
final HttpRequest httpRequest = new HttpRequest();
new Thread(new Runnable() {
@Override
public void run() {
String json;
try {
json = httpRequest.sendGet("url");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
现在返回json检查它是否有效。感谢。