我在以下文件夹中有一个 php 文件:
C:\wamp\www\android_test\fetch_data2
我需要能够从我的应用程序访问此文件,但目前无法访问它。这是我的 Java 代码,我将突出显示我需要修复的地方。我可以通过 localhost/android_data 从我的计算机访问 php 文件......,但我无法从我的手机浏览器访问它。此外,我没有在随附的 xml 文件中编写代码,也许这就是我的 php 文件没有显示在应用程序上的原因。
附加说明:我只想让我的应用程序显示这个连接到 mysql 数据库并输出的 php 文件:
[{"name":"User1","received_power":"-75.12207037710479","tower":"4","status":"0"},{"name":"User2","received_power":"18.89454151068304","tower":"3","status":"0"}]
我的名为 MySQLData.java 的 java 文件有以下内容:
package com.example.qosmetre2;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MySQLData extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String result = null;
InputStream is = null;
StringBuilder sb=null;
//String result=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://174.112.97.111/android_test/fetch_data2.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//paring data
String nam;
int rec_pow;
int tow;
int stat;
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
nam=json_data.getString("name");
rec_pow=json_data.getInt("recieved_power");
tow=json_data.getInt("tower");
stat=json_data.getInt("status");
}
}catch(JSONException e1){
Toast.makeText(getBaseContext(), "Could not Parse Data", Toast.LENGTH_LONG).show();
}catch (ParseException e1){
e1.printStackTrace();
}
}
}
还有一个名为 MainActivity.java 的文件,除了默认的东西,里面什么都没有,还有一个叫做 main_activity.xml 的 xml 文件,里面什么都没有。
这是我的 andoidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.qosmetre2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.qosmetre2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MySQLData"
android:label="All the Data" >
</activity>
</application>