我正在开发Android应用程序,其中应用程序与mysql和php连接。
数据库使用http://10.0.2.2/file.php
地址与模拟器完美连接。
但是当我将这个应用程序与我的Android设备连接时,它无法正常工作。
对于Android设备,我使用笔记本电脑的ip-address http://192.168.0.XX/file.php。
我在笔记本电脑中使用ubuntu操作系统。
首先加载活动,填写表单,当我点击提交按钮时,应用程序停止工作并显示一个对话框,显示:
应用无效。你想关闭吗? 以下代码有什么问题, 这是我的android代码:
MainActivity.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity implements android.view.View.OnClickListener
{
Button submit,button;
EditText et1,et2,et3;
String url;
CheckBox c1;
InputStream is=null;
String result=null;
String line=null;
int code;
String Uname=null;
String Email=null;
String Mobile=null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
et3=(EditText)findViewById(R.id.editText3);
submit=(Button)findViewById(R.id.button1);
submit.setOnClickListener(this);
button=(Button)findViewById(R.id.button2);
button.setOnClickListener(this);
}
@SuppressLint("NewApi")
@Override
public void onClick(View v)
{
if(v==submit)
{
Uname=et1.getText().toString();
Email=et2.getText().toString();
Mobile=et3.getText().toString();
if(Uname.isEmpty() || Uname.length()<0 ||
Email.isEmpty() || Email.length()<0 ||
Mobile.isEmpty() || Mobile.length()<0 )
{
Toast t=Toast.makeText(getApplicationContext(),"Please fill all the fields",Toast.LENGTH_LONG);
t.show();
}
else
{
insert();
Toast.makeText(getBaseContext(), "Password has been sent to your mobile",Toast.LENGTH_SHORT).show();
et1.setText("");
et2.setText("");
et3.setText("");
Intent i1=new Intent(this,LoginActivity.class);
startActivity(i1);
}
}
else if(v==button)
{
Intent i1=new Intent(this,LoginActivity.class);
startActivity(i1);
}
}
public void insert()
{
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost("http://192.168.0.XXX/file.php");
try
{
ArrayList<NameValuePair>pairs=new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("Uname", Uname));
pairs.add(new BasicNameValuePair("Email",Email));
pairs.add(new BasicNameValuePair("Mobile",Mobile));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is=entity.getContent();
Log.e("Success","Connection Success");
}
catch(Exception ex)
{
Log.e("Failed",ex.toString());
}
try
{
BufferedReader r1=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
while((line=r1.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
result=sb.toString();
Log.e("Result", result);
Log.e("Success Again", "Connection success-2");
}
catch(Exception ex1)
{
Log.e("Failed Again...","Unsuccess");
}
}
}
答案 0 :(得分:1)
如果您正在使用wamp,则可以编辑wamp \ bin \ apache \ apache2.4.9 \ conf \下的httpd.conf文件,将“require local”替换为“require all granted”
答案 1 :(得分:0)
我认为你必须检查你的笔记本电脑防火墙或网络限制,如果启用了,那就禁用它们。
答案 2 :(得分:0)
将cmd中的ip检查为ipconfig 使用此IP例如“http:\ 192.168.1.1 \ getlist.php”
主要关闭Windows防火墙