- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序是这样工作的。首先,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_name
和 R.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/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!