gpt4 book ai didi

java - 无法在我的 Android 应用程序中从 mysql 数据库获取数据到 ListView

转载 作者:行者123 更新时间:2023-11-29 00:02:23 25 4
gpt4 key购买 nike

我的应用程序是这样工作的。首先,MainActivity.java 显示从 mysql 数据库中获取的电影列表。单击列表中的电影时,SongsActivity.java 打开并显示该特定电影中的歌曲列表。第一个 Activity 工作正常,但是当我点击电影时,第二个 Activity 打开空白!没有提取正在完成!下面是我的代码,请帮助我。

MainActivity.java

package com.example.telugump3;
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.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
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;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity{

Activity context;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
ProgressDialog pd;
CustomAdapter adapter;
ListView listProduct;
ArrayList<String> records;

protected void onCreate(Bundle savedInstanceState) {

//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
records=new ArrayList<String>();
listProduct=(ListView)findViewById(R.id.product_list);
adapter=new CustomAdapter(context, R.layout.list_item,R.id.pro_name, records);
listProduct.setAdapter(adapter);
listProduct.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){

String sText = ((TextView) v.findViewById(R.id.pro_name)).getText().toString();
Intent songIntent = new Intent(getApplicationContext(), SongActivity.class);
songIntent.putExtra("movie_name", sText );
startActivity(songIntent);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
//create a LayoutTransition object
return true;
}

public void onStart(){

super.onStart();
//execute background task
BackTask bt=new BackTask();
bt.execute();
}

//background process to make a request to server and list product information
private class BackTask extends AsyncTask<Void,Void,Void>{

protected void onPreExecute(){

super.onPreExecute();
pd = new ProgressDialog(context);
pd.setTitle("Retrieving data");
pd.setMessage("Please wait.");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
}

protected Void doInBackground(Void...params){

InputStream is=null;
String result="";
try{

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://necrecords.16mb.com/getproducts.php");
response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){

if(pd!=null)
pd.dismiss(); //close the dialog if error occurs
Log.e("ERROR",e.getMessage());
}

//convert response to string
try{

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();
result=sb.toString();
}catch(Exception e){

Log.e("ERROR", "Error converting result "+e.toString());
}

//parse json data
try{

JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){

JSONObject json_data = jArray.getJSONObject(i);
String record = json_data.getString("pname")+"__"+json_data.getInt("uprice");
records.add(record);
}
}
catch(Exception e){

Log.e("ERROR", "Error pasting data "+e.toString());
}
return null;
}

protected void onPostExecute(Void result){

if(pd!=null) pd.dismiss(); //close dialog
adapter.notifyDataSetChanged(); //notify the ListView to get new records
}
}
}

SongsActivity.java

package com.example.telugump3;
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.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
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;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class SongActivity extends Activity{

Activity context;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
ProgressDialog pd;
CustomAdapter adapter;
ListView listProduct;
ArrayList<String> records;
String mname;

protected void onCreate(Bundle savedInstanceState) {

//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.song_activity);
context=this;
records=new ArrayList<String>();
listProduct=(ListView)findViewById(R.id.product_list);
adapter=new CustomAdapter(context, R.layout.newlist_item,R.id.pro_name, records);
listProduct.setAdapter(adapter);
Intent iin= getIntent();
Bundle b = iin.getExtras();

if(b!=null) {

mname =(String) b.getString("movie_name");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
//create a LayoutTransition object
return true;
}

public void onStart(){

super.onStart();
//execute background task
BackTask bt=new BackTask();
bt.execute();
}

//background process to make a request to server and list product information
private class BackTask extends AsyncTask<Void,Void,Void>{

protected void onPreExecute(){

super.onPreExecute();
pd = new ProgressDialog(context);
pd.setTitle("Retrieving data");
pd.setMessage("Please wait.");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
}

protected Void doInBackground(Void...params){

InputStream is=null;
String result="";
try{

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://necrecords.16mb.com/getsongslist.php?password="+mname);
response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){

if(pd!=null)
pd.dismiss(); //close the dialog if error occurs
Log.e("ERROR",e.getMessage());
}

//convert response to string
try{

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();
result=sb.toString();
}catch(Exception e){

Log.e("ERROR", "Error converting result "+e.toString());
}
//parse json data
try{

JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){

JSONObject json_data = jArray.getJSONObject(i);
String record = json_data.getString("allsongs")+"__"+json_data.getInt("test");
records.add(record);
}
}
catch(Exception e){

Log.e("ERROR", "Error pasting data "+e.toString());
}
return null;
}

protected void onPostExecute(Void result){

if(pd!=null) pd.dismiss(); //close dialog
adapter.notifyDataSetChanged(); //notify the ListView to get new records
}
}
}

CustomAdapter.java

package com.example.telugump3;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> {

int groupid;
ArrayList<String> records;
Context context;

public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){

super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;
}

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
String[] row_items=records.get(position).split("__");
TextView textName= (TextView) itemView.findViewById(R.id.pro_name);
textName.setText(row_items[0]);
TextView textPrice= (TextView) itemView.findViewById(R.id.pro_uprice);
textPrice.setText(row_items[1]+"$");
return itemView;
}
}

日志

03-22 22:54:02.500: E/IMGSRV(7557): :0: PVRDRMOpen: TP3, ret = 37
03-22 22:54:02.500: E/IMGSRV(7557): :0: PVRDRMOpen: TP3, ret = 38
03-22 22:54:02.510: E/IMGSRV(7557): :0: PVRDRMOpen: TP3, ret = 38
03-22 22:54:02.510: E/IMGSRV(7557): :0: PVRDRMOpen: TP3, ret = 38
03-22 22:54:04.710: E/Launcher.Model(22911): Widget ComponentInfo{com.asus.calendar/com.android.calendar.widget2.CalendarAppWidgetPadProvider} has invalid dimensions (0, 0)
03-22 22:54:05.060: E/NetworkScheduler.SchedulerReceiver(16209): Invalid parameter app
03-22 22:54:05.060: E/NetworkScheduler.SchedulerReceiver(16209): Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
03-22 22:54:05.600: E/IMGSRV(7586): :0: PVRDRMOpen: TP3, ret = 37
03-22 22:54:05.600: E/IMGSRV(7586): :0: PVRDRMOpen: TP3, ret = 38
03-22 22:54:05.600: E/IMGSRV(7586): :0: PVRDRMOpen: TP3, ret = 38
03-22 22:54:05.600: E/IMGSRV(7586): :0: PVRDRMOpen: TP3, ret = 38
03-22 22:54:05.790: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 62
03-22 22:54:05.840: E/Save(22911): com.android.launcher3.Workspace$$Icicle.
03-22 22:54:05.850: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 41
03-22 22:54:05.900: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 37
03-22 22:54:06.060: E/IMGSRV(7654): :0: PVRDRMOpen: TP3, ret = 46
03-22 22:54:06.070: E/IMGSRV(7654): :0: PVRDRMOpen: TP3, ret = 50
03-22 22:54:06.070: E/IMGSRV(7654): :0: PVRDRMOpen: TP3, ret = 51
03-22 22:54:06.070: E/IMGSRV(7654): :0: PVRDRMOpen: TP3, ret = 51
03-22 22:54:06.070: E/IMGSRV(7654): :0: PVRDRMOpen: TP3, ret = 51
03-22 22:54:06.090: E/IMGSRV(7654): :0: PVRDRMOpen: TP3, ret = 53
03-22 22:54:06.130: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 40
03-22 22:54:06.190: E/[ParseTask](2945): [doInBackground] fail to retrieve url: https://play.google.com/store/apps/details?id=com.example.telugump3&hl=en-US
03-22 22:54:06.210: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 50
03-22 22:54:06.300: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 55
03-22 22:54:06.320: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 62
03-22 22:54:06.330: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 36
03-22 22:54:06.340: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 66
03-22 22:54:06.680: E/NetworkScheduler.SchedulerReceiver(16209): Invalid parameter app
03-22 22:54:06.680: E/NetworkScheduler.SchedulerReceiver(16209): Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
03-22 22:54:07.150: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 40
03-22 22:54:07.340: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 41
03-22 22:54:07.820: E/lights(560): [LED] open path fail.
03-22 22:54:08.000: E/ERROR(7654): Illegal character in query at index 54: http://necrecords.16mb.com/getsongslist.php?password=A AA E EEE
03-22 22:54:08.000: E/ERROR(7654): Error converting result java.lang.NullPointerException: lock == null
03-22 22:54:08.000: E/ERROR(7654): Error pasting data org.json.JSONException: End of input at character 0 of
03-22 22:54:08.040: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 37
03-22 22:54:08.060: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 55
03-22 22:54:08.110: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 53
03-22 22:54:08.450: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 40
03-22 22:54:10.810: E/IMGSRV(185): :0: PVRDRMOpen: TP3, ret = 34
03-22 22:54:10.830: E/lights(560): [LED] open path fail.

现在我又多了两个疑惑。

1) 在 SongsActivity.java 中,我使用了从链接中的第一个 Activity 获得的字符串来获取所需的信息。这是在 url 中使用字符串的正确方法吗?

2) 当我按下后退按钮返回到第一个 Activity 时,ListView 再次获取数据并且电影列表加倍。有解决办法吗?

最佳答案

这里发生了一些事情,我会尽力解决。

这个日志条目是可疑的:

03-22 22:30:42.860: E/ERROR(6055): Illegal character in query at index 53: http://necrecords.16mb.com/getproducts.php?password=A AA E EEE

此外,从这个日志:

03-22 22:54:08.000: E/ERROR(7654): Error converting result java.lang.NullPointerException: lock == null

您可以看到您没有从该 PHP 页面获得有效响应。

此代码块抛出异常:

//convert response to string
try{
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();
result=sb.toString();
}catch(Exception e){
Log.e("ERROR", "Error converting result "+e.toString());

我刚刚在浏览器中尝试了该 URL,它成功了,因为浏览器会自动正确地对 url 进行编码,如下所示:

http://necrecords.16mb.com/getproducts.php?password=A%20AA%20E%20EEE

我认为您只需要正确编码 URL,因为该字符串包含空格。

String query = URLEncoder.encode(mname, "utf-8");

httppost= new HttpPost("http://necrecords.16mb.com/getsongslist.php?password="+query);
response=httpclient.execute(httppost);

至于列表加倍的问题,您可以在每次执行 AsyncTask 时清除列表:

protected Void doInBackground(Void...params){ 

InputStream is=null;
String result="";
try{

records.clear(); //clear the list before re-populating

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://necrecords.16mb.com/getproducts.php");
response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

}catch(Exception e){

还有一件事要提,您可能希望为每个 Acitvity 创建一个单独的适配器。就像现在一样,您正在为两个 Activity 使用相同的适配器。

在适配器的 getView() 中,您引用了 R.id.pro_nameR.id.pro_uprice,但您正在使用这个适配器在你的两个 Activity 中。您的两个 Activity 都在其布局 xml 中包含这些元素吗?

    public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
String[] row_items=records.get(position).split("__");
TextView textName= (TextView) itemView.findViewById(R.id.pro_name);
textName.setText(row_items[0]);
TextView textPrice= (TextView) itemView.findViewById(R.id.pro_uprice);
textPrice.setText(row_items[1]+"$");
return itemView;
}

关于java - 无法在我的 Android 应用程序中从 mysql 数据库获取数据到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197258/

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