-2

在此处输入图像描述我已经尝试了几个小时向 php 发送 POST 请求,该 php 发送回 json 响应。但是那一行代码一直给我带来问题,我似乎无法继续前进。我很沮丧。我已经查找了无数使用同一行的其他示例!但由于某种原因,它对我不起作用!这是我的 doinBackgrounds 的代码片段:

protected Void doInBackground(Void... voids) {
    HttpClient httpClient=new DefaultHttpClient();
    HttpGet request=new HttpGet();
    BufferedReader in=null;
    String data=null;
    HttpResponse response = null;
    try {
        URI website=new URI("login.php");
        request.setURI(website);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    HttpPost httpPost=new HttpPost("login.php");
    List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("name", name));
    nameValuePairs.add(new BasicNameValuePair("password", pwd));
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    try {
        response=httpClient.execute(request);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        String line=in.readLine();
        System.out.println(line);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (Utility.isNotNull(name) && Utility.isNotNull(pwd)) {
        RequestParams params = new RequestParams();
        if (Utility.validate(name, pwd)) {
            params.put("username", name);
            params.put("password", pwd);
            bloggedIn=true;
            onPostExecute();
        } else {
            loginActivity.InvalidToast();
        }
    } else {
        loginActivity.EmptyToast();
    }
    return null;
}
4

1 回答 1

0

你在呼唤doInBackground()自己。这不是你使用的方式AsyncTask。要执行AsyncTask,请创建一个实例并在其上调用execute()or executeOnExecutor()。这将导致doInBackground()在后台线程上运行。

AsyncTask您可以在JavaDocs中阅读更多信息。

(顺便说一句,将来,请将堆栈跟踪作为从 LogCat 复制的文本发布,而不是屏幕截图)

于 2015-11-09T22:58:23.920 回答