我有一个 android 应用程序,我希望它检查用户输入的 IP 地址是否可用,或者可以连接到,或者有一个 Apache 实例,我可以从中加载 php 文件。
这是我在活动的 java 文件中的代码:
package com.thesis.menubook;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class IPAddress extends Activity {
String ip;
String error = "";
Boolean next = false;
EditText ipaddress1;
EditText ipaddress2;
EditText ipaddress3;
EditText ipaddress4;
Button connect;
// Progress Dialog
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ipaddress);
connect = (Button) findViewById(R.id.connectBtn);
connect.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
ipaddress1 = (EditText) findViewById(R.id.ipAddress1);
ipaddress2 = (EditText) findViewById(R.id.ipAddress2);
ipaddress3 = (EditText) findViewById(R.id.ipAddress3);
ipaddress4 = (EditText) findViewById(R.id.ipAddress4);
ip = ipaddress1.getText().toString() +"."+ipaddress2.getText().toString()+"."+ipaddress3.getText().toString()+"."+ipaddress4.getText().toString();
new checkIPAddress().execute("");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_ipaddress, menu);
return true;
}
/**
* Background Async Task to Insert IP address
* */
class checkIPAddress extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(IPAddress.this);
pDialog.setMessage("Connecting to Server. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* check ipaddress connection in background thread
* */
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
pDialog.setMessage("Checking connection. Please wait...");
HttpClient httpclient = new DefaultHttpClient();
// CHECK IF USER HAS WIFI ENABLED
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
try {
HttpGet httpGet = new HttpGet(ip);
HttpResponse response = httpclient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
Log.d("Response", response.toString() +"--response from apache");
if(response.getStatusLine().getStatusCode() == 200) {
next = true;
} else {
next = false;
error = "Please check entered IP Address";
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
next = false;
error = "Please check entered IP Address";
}
} else {
next = false;
error = "No Internet connections available";
}
// CHECK IF USER CAN CONNECT TO APACHE SERVER
Log.d("IP Address", ip);
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
Log.d("NEXT VALUE --in Async on postexecute", next +"");
if(next == true)
{
Toast.makeText(IPAddress.this, "Connection succesful." , Toast.LENGTH_LONG).show();
Intent i = new Intent (IPAddress.this, ChooseTable.class);
i.putExtra("IP_Address", ip);
startActivity(i);
finish();
}
else
{
Toast.makeText(IPAddress.this, error, Toast.LENGTH_LONG).show();
}
}
}
}
这是我的 LogCat :
02-07 00:03:30.011: V/TLINE(668): new: android.text.TextLine@4066afd0
02-07 00:03:32.542: V/TLINE(668): new: android.text.TextLine@40674158
02-07 00:04:09.382: D/dalvikvm(668): GC_FOR_ALLOC freed 163K, 5% free 6483K/6791K, paused 236ms
02-07 00:04:09.448: I/dalvikvm-heap(668): Grow heap (frag case) to 6.929MB for 513744-byte allocation
02-07 00:04:09.962: D/dalvikvm(668): GC_CONCURRENT freed 2K, 5% free 6983K/7303K, paused 35ms+64ms
02-07 00:04:11.352: W/System.err(668): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=192.168.10.149
02-07 00:04:11.393: W/System.err(668): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
02-07 00:04:11.393: W/System.err(668): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-07 00:04:11.402: W/System.err(668): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-07 00:04:11.415: W/System.err(668): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-07 00:04:11.422: W/System.err(668): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-07 00:04:11.422: W/System.err(668): at com.thesis.menubook.IPAddress$checkIPAddress$1.run(IPAddress.java:99)
02-07 00:04:11.437: W/System.err(668): at android.os.Handler.handleCallback(Handler.java:587)
02-07 00:04:11.442: W/System.err(668): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 00:04:11.442: W/System.err(668): at android.os.Looper.loop(Looper.java:132)
02-07 00:04:11.462: W/System.err(668): at android.app.ActivityThread.main(ActivityThread.java:4025)
02-07 00:04:11.462: W/System.err(668): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 00:04:11.462: W/System.err(668): at java.lang.reflect.Method.invoke(Method.java:491)
02-07 00:04:11.482: W/System.err(668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
02-07 00:04:11.482: W/System.err(668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
02-07 00:04:11.492: W/System.err(668): at dalvik.system.NativeStart.main(Native Method)
02-07 00:04:11.492: D/IP Address(668): 192.168.10.149
02-07 00:04:11.503: D/NEXT VALUE --in Async on postexecute(668): false
我的笔记本电脑上有 Apache 并正在运行,我可以使用这台笔记本电脑的浏览器和我的安卓手机浏览器使用这台笔记本电脑的 IP 地址连接到它。我在模拟器上运行应用程序。My Anti Virus 也没有防火墙或网络防护。我已经在笔记本电脑的防火墙上打开了端口。我也尝试过运行应用程序并在ip
字符串中包含端口,但无济于事。