我的电脑ip是192.168.1.33。我想访问http://192.168.1.33//mla.php返回的JOSN文件。 我正在连接我的Android手机,而且我没有使用任何模拟器。我正在使用AsyncTask来获取SplashScreenActivity中的数据。我已经更改了apache的配置文件以允许所有甚至phpmyadmin页面的配置文件允许所有设备。我在JSONParser类中解析此URL。如何访问我的Android手机http://192.1638.1.33/mla.php?
//activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/splash" />
</RelativeLayout>
//JSONParser.java
package com.example.hellomla;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
//SpalshScreenActivity.java
package com.example.hellomla;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
public class SplashScreen extends Activity {
ArrayList<String> consts = new ArrayList<String>();//array to store constituency names
ArrayList<String> cids = new ArrayList<String>();//array to store constituency ids
ArrayList<String> cps=new ArrayList<String>();//array to store constituency population
ArrayList<String> conslks=new ArrayList<String>();//array to store loksabha constituency
ArrayList<String> conmp=new ArrayList<String>();
ArrayList<String> mlaname=new ArrayList<String>();
ArrayList<String> mlaparty=new ArrayList<String>();
ArrayList<String> mlaage=new ArrayList<String>();
ArrayList<String> mlaedu=new ArrayList<String>();
ArrayList<String> mlaaddress=new ArrayList<String>();
ArrayList<String> mlaemail=new ArrayList<String>();
ArrayList<String> mlaphone=new ArrayList<String>();
ArrayList<String> mlawebsite=new ArrayList<String>();
ArrayList<String> mlaabout=new ArrayList<String>();
ArrayList<String> mlaphoto=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String,String,JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SplashScreen.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
// before making http calls
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = jsonParser.getJSONFromUrl("http://192.168.1.33/constitncy.php");
return jsonObj;
}
protected void onPostExecute(JSONObject jsonObj) {
//super.execute(result);
// After completing http call
// will close this activity and lauch main activity
pDialog.dismiss();
try {
JSONArray jsonArray = jsonObj.getJSONArray("constituencies");
for (int i = 0; i < jsonArray.length(); i++) {
// add constituency name to consts array
consts.add(jsonArray.getJSONObject(i).getString("conName"));
// add constituency id to cids array
cids.add(jsonArray.getJSONObject(i).getString("conID"));
cps.add(jsonArray.getJSONObject(i).getString("conPopulation"));
conslks.add(jsonArray.getJSONObject(i).getString("conLokSabha"));
conmp.add(jsonArray.getJSONObject(i).getString("conMP"));
mlaname.add(jsonArray.getJSONObject(i).getString("mlaName"));
mlaparty.add(jsonArray.getJSONObject(i).getString("mlaParty"));
mlaage.add(jsonArray.getJSONObject(i).getString("mlaAge"));
mlaedu.add(jsonArray.getJSONObject(i).getString("mlaEdu"));
mlaaddress.add(jsonArray.getJSONObject(i).getString("mlaAddress"));
mlaemail.add(jsonArray.getJSONObject(i).getString("mlaEmail"));
mlaphone.add(jsonArray.getJSONObject(i).getString("mlaPhone"));
mlawebsite.add(jsonArray.getJSONObject(i).getString("mlaWebsite"));
mlaabout.add(jsonArray.getJSONObject(i).getString("mlaAbout"));
mlaphoto.add(jsonArray.getJSONObject(i).getString("mlaPhoto"));
}
} catch (Exception e) {
e.printStackTrace();
}
Intent i = new Intent(SplashScreen.this, MainActivity.class);
i.putExtra("cIDS", cids);
i.putExtra("cNAMES", consts);
i.putExtra("cPLTN",cps);
i.putExtra("cLKS",conslks);
i.putExtra("cMP",conmp);
i.putExtra("mNAME",mlaname);
i.putExtra("mPARTY",mlaparty);
i.putExtra("mAGE",mlaage);
i.putExtra("mEDU",mlaedu);
i.putExtra("mADRS",mlaaddress);
i.putExtra("mEML",mlaemail);
i.putExtra("mPHNE",mlaphone);
i.putExtra("mWBST",mlawebsite);
i.putExtra("mABT",mlaabout);
i.putExtra("mPHTO",mlaphoto);
startActivity(i);
// close this activity
finish();
}
}
}