0

我正在尝试开发使用 Web 服务的简单应用程序。我在mysql中有登录屏幕和数据库。但是当我运行代码时,它会抛出套接字异常。我哪里出错了。我还想知道如何将数据放入来自 Web 服务的变量中。

这是我的清单文件

 <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>

和我的活动文件

package com.example.webserviceapplication;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener
{

EditText username,password;
Button login,cancel;
TextView msg;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    username=(EditText)findViewById(R.id.username);
    password=(EditText)findViewById(R.id.password);

    login=(Button)findViewById(R.id.login);
    cancel=(Button)findViewById(R.id.cancel);

    msg=(TextView)findViewById(R.id.msg);

    login.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            try 
            {
                HttpClient client = new DefaultHttpClient();  
                String postURL = "http://gladiolus/TestPhp/android.php";
                HttpPost post = new HttpPost(postURL);
                HttpResponse responsePOST = client.execute(post);  
                HttpEntity resEntity = responsePOST.getEntity();  
                String response=EntityUtils.toString(resEntity);
                response=response.trim();
                msg.setText(response);
                Log.i("RESPONSE=",response);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}



 }

和我的 php 文件

<?php
$con = mysql_connect("localhost","","");
if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

mysql_select_db("android_test_db", $con);

$result = mysql_query("SELECT * FROM login");

while($row = mysql_fetch_array($result))
  {
  echo $row['username'] . " " . $row['password'];
  echo "<br />";
  }

print_r($row);

mysql_close($con);
?>

和 php 的路径是剑兰/TestPhp/android.php

4

1 回答 1

0

一件事你的网址不正确。要在本地主机上测试它,您需要使用地址作为

http://10.0.2.2:80/gladiolus/TestPhp/android.php

在这里阅读更多信息。

于 2012-10-26T11:34:26.543 回答