Java Android应用程序不显示来自Web服务器的数据
我想开发一个android应用程序,它从web服务器(我在www.byethost.com上的免费域帐户)获取数据,并在我的android应用程序的列表视图中显示数据。过去5天我遇到的问题是,当我在本地主机(WAMP)上运行我的PHP脚本并使用此链接从emulator调用它时,它运行得很好。但当我将此脚本放在免费域面板上并从此链接调用时,它不会在我使用USB电缆连接到PC的真实设备上显示任何内容。自从过去5天以来,我已经搜索了很多链接,但它没有显示任何与我的问题相关的信息。先谢谢你 MainActivity.javaJava Android应用程序不显示来自Web服务器的数据,java,php,android,mysql,Java,Php,Android,Mysql,我想开发一个android应用程序,它从web服务器(我在www.byethost.com上的免费域帐户)获取数据,并在我的android应用程序的列表视图中显示数据。过去5天我遇到的问题是,当我在本地主机(WAMP)上运行我的PHP脚本并使用此链接从emulator调用它时,它运行得很好。但当我将此脚本放在免费域面板上并从此链接调用时,它不会在我使用USB电缆连接到PC的真实设备上显示任何内容。自从过去5天以来,我已经搜索了很多链接,但它没有显示任何与我的问题相关的信息。先谢谢你 MainAc
package com.example.abdul.zx;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity
{
String myJSON ;
String id;
String name;
private static final String TAG_RESULTS="result";
// private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// private static final String TAG_ADD ="address";
JSONArray peoples = null;
ArrayList <HashMap <String, String> > personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
// id = c.getString(TAG_ID);
name = c.getString(TAG_NAME);
// String address = c.getString(TAG_ADD);
HashMap<String,String> persons = new HashMap<String,String>();
// persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
// persons.put(TAG_ADD,address);
personList.add(persons);
}
final ListAdapter adapter = new SimpleAdapter
(MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_NAME},
new int[]{R.id.name});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch(i)
{
case 0 :
Intent appInfo = new Intent(MainActivity.this, Desserts.class);
startActivity(appInfo);
break;
case 1 :
Intent ap = new Intent(MainActivity.this, MainActivity3.class);
startActivity(ap);
break;
case 2 :
Intent Info = new Intent(MainActivity.this, Desserts.class);
startActivity(Info);
break;
}}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
//HttpPost httppost = new HttpPost("http://10.0.2.2/ch.php");
HttpPost httppost = new HttpPost("http://zamin.byethost14.com/demo.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
}
我尝试从浏览器访问API链接。您确定这是HTTP Post请求吗? 试试这个
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI("http://zamin.byethost14.com/demo.php");
request.setURI(website);
HttpResponse response = httpclient.execute(request);
任何异常或Logcat??post ur Logcat或post execute的字符串结果值…没有异常发生。没有输出仅显示背景。@RishadAppat看到我已使用LogcatEdit编辑我的问题运行应用程序后GetDataJSON onPostExecute方法中的'result'值是多少?。当我将POST更改为GET时,它显示此异常java.lang.NullPointerException:尝试调用虚拟方法'int java.lang.String.length()'在此行的空对象引用上,JSONObject jsonObj=new JSONObject(myJSON);您的响应格式是JSONArray。试试这个JSONArray jsonObj=newjsonarray(myJSON);我已经改变了这一点,但没有解决方案仍然是相同的问题MyJSON为空。它没有得到更新。在JSONObject jsonObj=newjsonobject(myJSON)之前确认这一点。这是因为您试图从内部类中修改类变量吗?请尝试以更好的方式将myJSon从AsyncTask传递到Activity。在AsyncTask中创建一个构造函数,该构造函数包含活动对象。现在通过myActivity.myJson=result更新myJson的值;
PropertyFetcher: AdbCommandRejectedException getting properties for device LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems wrong.
Otherwise check for a confirmation dialog on your device.
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI("http://zamin.byethost14.com/demo.php");
request.setURI(website);
HttpResponse response = httpclient.execute(request);