我已经建立了一个共享1个数据库的3个网站,并且已经添加了一个可用的JSON php文件。这些代码上次工作,现在我无法在我的Android应用程序中显示或显示来自网站的数据,我不明白为什么,因为我没有改变它的任何一个。错误发生在getNewsfeeds();
中Welcome.java
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Welcome extends Activity {
Button welcome;
AQuery aq;
JSONArray articles = new JSONArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
getNewsfeeds();
aq = new AQuery(this);
welcome = (Button) findViewById(R.id.welcome);
welcome.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), NewsFeed.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
public void getNewsfeeds(){
aq.ajax("http://sillimaninquirer.esy.es/TWS/sidata.php?key=none", JSONObject.class, new AjaxCallback<JSONObject>(){
@Override
public void callback(String url, JSONObject jo, AjaxStatus as){
try {
if(jo!=null){
articles = jo.getJSONArray("article");
Toast.makeText(getBaseContext(), "Articles: "+articles, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "Error: No internet connection", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
Manage.setArticles(articles);
}
});
}
}
NewsFeed.java
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class NewsFeed extends Activity {
ListView feed_listView;
EditText search;
Button t, a, d, c, k;
JSONArray articles = new JSONArray();
ArrayList<String> title = new ArrayList<String>();
ArrayList<String> content = new ArrayList<String>();
ArrayList<String> date = new ArrayList<String>();
ArrayList<String> cpid = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
addArticlesToVar();
search = (EditText) findViewById(R.id.search);
feed_listView = (ListView) findViewById(R.id.listView_NewsFeed);
final Adapter adapter = new Adapter(this,title, content, date,cpid);
feed_listView.setAdapter(adapter);
feed_listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
feed_listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
String t = title.get(position);
String c = content.get(position);
String d = date.get(position);
String cat = cpid.get(position);
Intent i = new Intent(v.getContext(), Article.class);
i.putExtra("t", t);
i.putExtra("c", c);
i.putExtra("d", d);
i.putExtra("cat", cat);
startActivity(i);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
});
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
}
public void AddPref(View v){
Intent i = new Intent(v.getContext(), AddPreference.class);
startActivity(i);
}
public void News(View v){
Intent i = new Intent(v.getContext(), NewsFeed.class);
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news_feed, menu);
return true;
}
public void addArticlesToVar(){
articles = Manage.getArticles();
for(int x=0; x<articles.length(); x++){
try {
JSONObject article = articles.getJSONObject(x);
title.add(article.getString("title"));
content.add(article.getString("content"));
date.add(article.getString("pdate"));
cpid.add(article.getString("cpid"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
class Adapter extends ArrayAdapter<String>{
Context context;
ArrayList<String> title, content, date, cpid;
//ArrayList<Articles> art1, art2;
Adapter (Context context, ArrayList<String> title,ArrayList<String> content, ArrayList<String> date, ArrayList<String>cpid){
super(context, R.layout.row,R.id.newsfeed_Title, title);
this.context = context;
this.content = content;
this.date = date;
this.title = title;
this.cpid = cpid;
}
public View getView(int position, View convertView, ViewGroup parent){
String t="", c="";
String tit="", con="";
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.row, parent, false);
TextView tv_title = (TextView) row.findViewById(R.id.newsfeed_Title);
TextView tv_content = (TextView) row.findViewById(R.id.newsfeed_Contentext);
TextView tv_date = (TextView) row.findViewById(R.id.newsfeed_date);
t = title.get(position);
c = content.get(position);
for(int tc=0; tc<13; tc++){
tit+=t.charAt(tc);
}
for(int cc=0; cc<30; cc++){
con+=c.charAt(cc);
}
tv_title.setText(tit);
tv_content.setText(con);
tv_date.setText("By "+cpid.get(position)+" "+date.get(position));
return row;
}
}
Manage.java
import org.json.JSONArray;
public class Manage {
//key=none
static JSONArray articles = new JSONArray();
public static void setArticles(JSONArray articles){
Manage.articles = articles;
}
public static JSONArray getArticles(){
return articles;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/newsfeed_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textStyle="bold"
android:paddingTop="5px"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/newsfeed_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="18px"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/newsfeed_Contentext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contentext"
android:paddingTop="5px"
android:textStyle="italic"
android:paddingLeft="25px"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
logcat的
05-20 05:24:04.035: E/bluestacksHome(9927): Failed to fetch map from web: http://opasanet.appspot.com/op/appmap?id=com.example.sillimaninquirer
05-20 05:24:04.035: E/bluestacksHome(9927): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
05-20 05:24:04.035: E/bluestacksHome(9927): at org.json.JSON.typeMismatch(JSON.java:111)
05-20 05:24:04.035: E/bluestacksHome(9927): at org.json.JSONObject.<init>(JSONObject.java:159)
05-20 05:24:04.035: E/bluestacksHome(9927): at org.json.JSONObject.<init>(JSONObject.java:172)
05-20 05:36:37.145: E/AndroidRuntime(11683): FATAL EXCEPTION: main
05-20 05:36:37.145: E/AndroidRuntime(11683): Process: com.example.sillimaninquirer, PID: 11683
05-20 05:36:37.145: E/AndroidRuntime(11683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sillimaninquirer/com.example.sillimaninquirer.Welcome}: java.lang.NullPointerException
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.os.Handler.dispatchMessage(Handler.java:102)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.os.Looper.loop(Looper.java:136)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.ActivityThread.main(ActivityThread.java:5021)
05-20 05:36:37.145: E/AndroidRuntime(11683): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 05:36:37.145: E/AndroidRuntime(11683): at java.lang.reflect.Method.invoke(Method.java:515)
05-20 05:36:37.145: E/AndroidRuntime(11683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
05-20 05:36:37.145: E/AndroidRuntime(11683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
05-20 05:36:37.145: E/AndroidRuntime(11683): at dalvik.system.NativeStart.main(Native Method)
05-20 05:36:37.145: E/AndroidRuntime(11683): Caused by: java.lang.NullPointerException
05-20 05:36:37.145: E/AndroidRuntime(11683): at com.example.sillimaninquirer.Welcome.getSIData(Welcome.java:53)
05-20 05:36:37.145: E/AndroidRuntime(11683): at com.example.sillimaninquirer.Welcome.onCreate(Welcome.java:29)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.Activity.performCreate(Activity.java:5231)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
05-20 05:36:37.145: E/AndroidRuntime(11683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-20 05:36:37.145: E/AndroidRuntime(11683): ... 11 more
答案 0 :(得分:1)
我想告诉您使用Volley Networking Library获取Json数据,然后相应地解析该数据。
解析后,您可以根据需要在listview或Recyclerview中填写该数据。