0

我有网络服务:“http://nclex.testshell.net/api/forums/1?type=json”放在“json lint”格式化程序中,但数据不是来自网络服务。但是这个webservice直接放在浏览器中然后数据就会来。这是一个Dot net webservice。但是以编程方式我得到了响应:“零”。这是我的异步任务类:

class ListDoback extends AsyncTask<URL, Integer, Long>{     
protected void onPreExecute() 
{
try
{
                                                            pgrDialog=MyProgressDialog.show(ForumsDetailsActivity.this, null,null);
}
catch(Exception e){}
}
protected Long doInBackground(URL... arg0) 
{
                       if(!CheckInternetConnection.isOnline(ForumsDetailsActivity.this))
{                   
pgrDialog.dismiss();                    
new AlertDialog.Builder(ForumsDetailsActivity.this).setTitle(DataUrls.dialogtitle)
                            .setMessage(DataUrls.dialogmsg)
                              .setPositiveButton(DataUrls.dialogbutton, new DialogInterface.OnClickListener() 
                            {                                                  public void onClick(DialogInterface dialog, int whichButton)  {
finish();       }
}).show();
cancel (true);
}
else
{
Log.e("forum id:",DataUrls.strForumId);

            strResponseReply=UrltoValue.getValuefromUrl(DataUrls.replyforum+DataUrls.strForumId+"type=json");
                               Log.e("check",DataUrls.replyforum+DataUrls.strForumId+"?type=json");
                            Log.e("response:",strResponseReply);

                            try {
                                JSONObject jsonObject=new JSONObject(strResponseReply);
                                JSONObject jObject=jsonObject.getJSONObject("data");
                                jsonArray=jObject.getJSONArray("Reply");

                                strReplyId=new String[jsonArray.length()];
                                strForumId=new String[jsonArray.length()];
                                strUserId=new String[jsonArray.length()];
                                strUserName=new String[jsonArray.length()];
                                strUserPicture=new String[jsonArray.length()];
                                strNickname=new String[jsonArray.length()];
                                strAgo=new String[jsonArray.length()];
                                strPostTopic=new String[jsonArray.length()];

                                for(int i=0;i<jsonArray.length();i++){
                                    strForumId[i]=jsonArray.getJSONObject(i).getString("ForumId");
                                    strUserId[i]=jsonArray.getJSONObject(i).getString("UserId");
                                    strUserName[i]=jsonArray.getJSONObject(i).getString("UserName");
                                    strNickname[i]=jsonArray.getJSONObject(i).getString("NickName");
                                    strAgo[i]=jsonArray.getJSONObject(i).getString("Ago");
                                    strUserPicture[i]=jsonArray.getJSONObject(i).getString("UserPicture");
                                    strPostTopic[i]=jsonArray.getJSONObject(i).getString("posttopic");
                                    strReplyId[i]=jsonArray.getJSONObject(i).getString("ReplyId");

                                    Log.e("Nick name:",strNickname[i]); 
                                    Log.e("post topic:",strPostTopic[i]);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }                 
                    return null;
                }
                protected void onProgressUpdate(Integer... progress) 
                {

                }
                protected void onPostExecute(Long result) 
                {
                    Log.e("postexecute","onpostexecute");
                    if(strResponseReply.equals("zero")||jsonArray.length()==0)
                    {
                        Toast.makeText(ForumsDetailsActivity.this, "No forums found", Toast.LENGTH_SHORT).show();
                    }
                    try
                    {

                        lvPostReply.setAdapter(new ForumsReplyList(ForumsDetailsActivity.this,R.layout.forumreplyitem,strNickname,strPostTopic,strUserPicture,strAgo));
                    }
                    catch(Exception e)
                    {
                        pgrDialog.dismiss();
                    }
                    pgrDialog.dismiss();
                    Log.e("hi","hi");
                }
            } //closing BarPicsDoback process.

请帮助我。在此先感谢。

4

2 回答 2

0

您无法使用“json lint”检查 .NET Web 服务是否是正确的 Json

于 2013-01-19T07:05:47.427 回答
0

在您的异步类中使用此代码,您将获得响应

try
{
     response = httpclient.execute(httpget);
     Log.e("Response", "Status:[" + response.getStatusLine().toString() + "]");
     Log.e("check",DataUrls.replyforum+DataUrls.strForumId+"?type=application/json");
     HttpEntity entity = response.getEntity();

     if (entity != null) 
     {                                              
        InputStream instream = entity.getContent();
        strResponseReply = RestClient.convertStreamToString(instream);
        Log.i("Result", "Result of converstion: [" + strResponseReply + "]");
        instream.close();
        return result;
     }
 } 
catch (ClientProtocolException e) 
{
     Log.e("REST", "There was a protocol based error", e);
}
catch (IOException e) 
{
     Log.e("REST", "There was an IO Stream related error", e);
}              
}
于 2013-01-23T11:40:53.600 回答