该方法假设从 JSON 数组中获取所需的字符串并将它们设置为各个文本视图的值,但 try 块不起作用..总是在 catch 块处停止......
这是课
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.nairation.library.JSONParser;
import com.nairation.library.NetworkUtil;
import com.nairation.library.UserFunctions;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class TopicView extends Activity{
private TextView tname;
private TextView uname;
private TextView tcontent;
private String val;
private String val1;
private ActionBar actionBar;
private LinearLayout Progress, nilinter;
private String gh;
private UserFunctions userf;
private JSONParser parser;
public static final String CONTENT = "content";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.topic_view);
tname = (TextView) findViewById(R.id.topic_name);
uname = (TextView) findViewById(R.id.topic_username);
tcontent = (TextView) findViewById(R.id.topic_content);
nilinter = (LinearLayout) findViewById(R.id.noInterLayout);
Progress = (LinearLayout) findViewById(R.id.topicProg);
val = getIntent().getExtras().getString("tname").toString();
val1 = getIntent().getExtras().getString("tid").toString();
tname.setText(val);
if(val.length() > 19){
gh = val.substring(0, 20) + "...";
}else{
gh = val;
}
//Action bar settings....
actionBar = getActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setTitle(gh);
final int actionBarColor = getResources().getColor(R.color.nGreen);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
int interStatus = NetworkUtil.getConnectivityStatus(getApplicationContext());
if(interStatus != 0){
new TopicSync().execute(Integer.parseInt(val1));
}else{
nilinter.setVisibility(View.VISIBLE);
}
}
private class TopicSync extends AsyncTask<Integer, JSONObject, JSONObject>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
Progress.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(Integer... params) {
// TODO Auto-generated method stub
int topicId = params[0];
userf = new UserFunctions();
JSONObject jp = userf.getTopicView(topicId);
return jp;
}
@Override
protected void onPostExecute(JSONObject result) {
try{
JSONArray feed = result.getJSONArray("data");
for (int i = 0; i < feed.length(); i++) {
JSONObject feeda = feed.getJSONObject(i);
String content = feeda.getString("user");
String username = feeda.getString("user");
uname.setText(username);
tcontent.setText(content);
publishProgress(feeda);
}
Progress.setVisibility(View.GONE);
}catch(JSONException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Problem with loading topic", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
return super.onOptionsItemSelected(item);
}
}
这是来自 PHP 文件的 JSON 对象
'{"data":[{"content":"<p><img src=\"https:\/\/ucarecdn.com\/9993f861-212b-4ee1-b89e-af6f414948f8\/\" \/><\/p>\n","user":"mash"}]}'
所以请问可能是什么问题
LOGCAT 输出
10-09 02:16:37.455: W/System.err(7420): org.json.JSONException: No value for user
10-09 02:16:37.465: W/System.err(7420): at org.json.JSONObject.get(JSONObject.java:354)
10-09 02:16:37.465: W/System.err(7420): at org.json.JSONObject.getString(JSONObject.java:514)
10-09 02:16:37.465: W/System.err(7420): at com.nairation.rss.TopicView$TopicSync.onPostExecute(TopicView.java:105)
10-09 02:16:37.465: W/System.err(7420): at com.nairation.rss.TopicView$TopicSync.onPostExecute(TopicView.java:1)
10-09 02:16:37.465: W/System.err(7420): at android.os.AsyncTask.finish(AsyncTask.java:631)
10-09 02:16:37.465: W/System.err(7420): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-09 02:16:37.465: W/System.err(7420): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-09 02:16:37.465: W/System.err(7420): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 02:16:37.465: W/System.err(7420): at android.os.Looper.loop(Looper.java:137)
10-09 02:16:37.465: W/System.err(7420): at android.app.ActivityThread.main(ActivityThread.java:5289)
10-09 02:16:37.465: W/System.err(7420): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 02:16:37.465: W/System.err(7420): at java.lang.reflect.Method.invoke(Method.java:525)
10-09 02:16:37.465: W/System.err(7420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-09 02:16:37.465: W/System.err(7420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-09 02:16:37.465: W/System.err(7420): at dalvik.system.NativeStart.main(Native Method)
10-09 02:17:10.350: W/ViewRootImpl(7420): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x48, repeatCount=0, eventTime=350819868, downTime=350819868, deviceId=10, source=0x101 }