OnPostExecute方法不解析JSON数组

时间:2013-10-09 01:02:00

标签: java android android-asynctask

该方法假设从JSON数组中获取所需的字符串并将它们设置为单个textviews的值,但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 }

1 个答案:

答案 0 :(得分:1)

根据有关JSONObject getString的引用,如果该键没有字符串值,它将抛出异常。这意味着没有“用户”存在。

 String content = feeda.getString("user");
 String username = feeda.getString("user");

因此,您最好调试JSONObject feeda = feed.getJSONObject(i);以检查feeda的值或将其注销以检查内部的内容。可能你的PHP文件没有正确读取。