我尝试开发使用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的路径是gladiolus / TestPhp / android.php
答案 0 :(得分:0)