gpt4 book ai didi

android - 无法从 php mysql 检索数据到 android Activity

转载 作者:行者123 更新时间:2023-11-29 21:34:28 24 4
gpt4 key购买 nike

我是 Android 编程新手。所以问题就在这里。我目前正在使用 php 框架 codeigniter 从 mysql 查询数据。当我在浏览器中输入 url 时,我会看到我的 json 数据。但是当我在 Android Activity 中检索它时,它返回 null。这是我的代码。PHP代码

function getdrug(){
$id = $_GET['letter'];
//echo $_GET['letter'];
header('Content-type: application/json');
if($id == 'a'){
$this->db->like('Generic_Name','A');
$value['Drugs'] = $this->db->get('drugs')->result();
echo json_encode($value);
}
}

我的 Android Activity

ListView lv;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String URL = "http://emedteam-001-site1.ctempurl.com/emed/home/getdrug?letter=a";
jsonobject = JSONfunctions.getJSONfromURL(URL);
Toast.makeText(getApplicationContext(),URL + "\n" +jsonobject, Toast.LENGTH_LONG).show(); }
});

我的 JSON 函数

public class JSONfunctions {

public static JSONObject getJSONfromURL(final String URL){
InputStream is = null;
String result = "";
String params = "";

JSONObject jArray = null;


try{

HttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpGet httpget = new HttpGet(URL);

HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}
catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//Convert
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();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
try {

jArray = new JSONObject(result);
System.out.print(jArray);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;

}
}

这是我的网址链接http://emedteam-001-site1.ctempurl.com/emed/home/getdrug?letter=a

最佳答案

HTTPClient 已被弃用,因此您应该避免使用它。看看这个HttpClient is deprecated (android studio, Target=api 22)

这是我从 json 获取内容的工作函数

    public static JSONObject getJSONfromURL(final String URL){

JSONObject jArray = null;
try {
//URL = "http://emedteam-001-site1.ctempurl.com/emed/home/getdrug?letter=a";
URLConnection conn = new URL(URL).openConnection();
conn.addRequestProperty("Accept", "application/json");
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();

String str = sb.toString();
sb = null;
jArray = new JSONObject(str);
} catch (Exception e) {
e.printStackTrace();
}
return jArray;
}

已编辑:您需要在 AsyncTask 中调用此函数以避免 How to fix android.os.NetworkOnMainThreadException?

ListView lv;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new StackOverflowTask().execute();
}
});

class StackOverflowTask extends AsyncTask<String, Void, JSONObject> {
protected JSONObject doInBackground(String... urls) {
try {
JSONObject test = JSONfunctions.getJSONfromURL("http://emedteam-001-site1.ctempurl.com/emed/home/getdrug?letter=a");
return test;
} catch (Exception e) {
return null;
}
}

protected void onPostExecute(JSONObject test) {
if(test != null)
Toast.makeText(TestActivity.this, "Hello Stackoverflow ;)",Toast.LENGTH_SHORT).show();
}
}

希望对你有帮助!

关于android - 无法从 php mysql 检索数据到 android Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com