【问题标题】:android httpclient.execute throwing exception on emulatorandroid httpclient.execute在模拟器上抛出异常
【发布时间】:2012-02-19 12:35:28
【问题描述】:

我正在尝试使用 httppost 连接到网页。但是下面的代码从httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));开始抛出异常。

我已经尝试了所有组合,但找不到问题。我该如何调试它?我对 android + java 世界还很陌生。

编辑: 为了避免任何问题,我只是按照此处给出的示例进行操作。

http://www.vogella.de/articles/AndroidNetworking/article.html

在第四部分。但它仍然给出同样的错误。有人可以帮忙吗? 谢谢。

编辑结束

package com.example.row;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpEntity;
import java.io.InputStream;
import android.widget.TextView;
import org.apache.http.util.EntityUtils;


public class mcrow3 extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        String result = "MC";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("myusername","user"));
        nameValuePairs.add(new BasicNameValuePair("mypassword","pwd"));
        TextView tv = new TextView(this);
        try
        {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://example.com/login.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        }
        catch(Exception e)
        {
            tv.setText("Some problem");
            setContentView(tv);
        }

        tv.setText(result);
        setContentView(tv);
    }
}

如果我做错了什么,请告诉我。在模拟器上安装它说不幸的是应用程序停止了。我错过了模拟器上的任何配置吗?我的模拟器的互联网连接在浏览器上工作。

编辑: 请找到服务器端的PHP脚本:

<?php

$host="host"; // Host name
$username="user"; // Mysql username
$password="pwd"; // Mysql password
$db_name="db"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE emailid='$myusername' and password='$mypassword'";
while($e=mysql_fetch_assoc($sql))
        $output[]=$e;

print(json_encode($output));
mysql_close();
?>

【问题讨论】:

    标签: java android httpclient http-post


    【解决方案1】:

    虽然 httppost 实体设置为 URLEncodedFormEntity,但可能您还需要在 http 标头内容类型中指定为:Content-Type: application/x-www-form-urlencoded 。

    也可能是因为您在与 UI 相同的线程上进行网络调用。

    【讨论】:

      【解决方案2】:

      如下设置参数和发布实体:

      String jsonParam = null;
      try{
          JSONObject param = new JSONObject();
          param.put("myusername", "user");
          param.put("mypassword", "pwd");
          //and so on with other parameters
      
          jsonParam = param.toString();
      }
      catch (Exception e) {
          // TODO: handle exception
      }
      
      httppost.setEntity(new StringEntity(jsonParam, HTTP.UTF_8));
      

      【讨论】:

      • 嗨,Waqas,我试过了,但结果发生了变化。我也粘贴了服务器端的 PHP 代码。你能帮忙吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      相关资源
      最近更新 更多