gpt4 book ai didi

java - android httpclient.execute 在模拟器上抛出异常

转载 作者:行者123 更新时间:2023-11-30 04:20:22 25 4
gpt4 key购买 nike

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

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

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

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

在第 4 节中。但它仍然给出同样的错误。任何人都可以帮忙吗?谢谢。

编辑结束

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

最佳答案

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

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

关于java - android httpclient.execute 在模拟器上抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9349187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com