Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android测试类_Java_Unit Testing_Junit_Android - Fatal编程技术网

Java Android测试类

Java Android测试类,java,unit-testing,junit,android,Java,Unit Testing,Junit,Android,我有一个HttpRequestActivity类,它从android应用程序调用服务器。我必须为这个类编写一个测试用例来检查收到的字符串是否是JSON字符串。我应该怎么做。我已经尝试实现了这个方法 public class HttpRequestActivityTest extends android.test.ActivityUnitTestCase<HttpRequestActivity> { 它给出的错误是HttpRequestActivity不是活动,因此不能作为Activ

我有一个HttpRequestActivity类,它从android应用程序调用服务器。我必须为这个类编写一个测试用例来检查收到的字符串是否是JSON字符串。我应该怎么做。我已经尝试实现了这个方法

public class HttpRequestActivityTest extends
android.test.ActivityUnitTestCase<HttpRequestActivity> {
它给出的错误是HttpRequestActivity不是活动,因此不能作为ActivityUnitTestCase的参数,这是正确的

应该采取什么办法

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;
    }

}

  }
使用以下命令:

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;
    }

}
现在要返回json,请检查它是否有效。谢谢

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();