Android help need with json -


i'm in need of little encoding json string receive data web service. tried format json string standard way see below in jsonparsser class told json string invalid can see here in older question. converting string jsonobject i'm not asking code me i'm asking can @ code , let me know if there errors url parameters , should encode json string receive data web service. here link url parameters http://developer.yahoo.com/answers/v1/questionsearch.html , see data i'm trying parse @ json data not class of question https://stackoverflow.com/questions/18028570/yahoo-answers-json-parse

mainactivity:

public class mainactivity extends activity {      button getanswer;        @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);            button getanswer = (button) findviewbyid(r.id.button1);         getanswer.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {                    edittext et = (edittext) findviewbyid(r.id.edittext1);             string searchterm = et.gettext().tostring().trim();                      intent in = new intent(mainactivity.this, listview.class);             in.putextra("tag_search", searchterm);             startactivity(in);         }           });     }} 

listview activity:

public class listview extends listactivity {           arraylist<hashmap<string, string>> questionlist;           final string tag_results = "results";      final string tag_question_subject = "subject";      final string tag_question_numanswers = "numanswers";      final string tag_question = "question";      final string tag_question_content = "content";      final string tag_question_chosenanswer = "chosenanswer";      final string tag_answers = "answers";      final string tag_answer = "answer";          final string tag_answers_content = "content";                    jsonarray results;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);          setcontentview(r.layout.listview);                 //questionlist = new arraylist<hashmap<string, string>>();        new loadalldata().execute();         }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (resultcode == 100) {             intent intent = getintent();             startactivity(intent);             finish();         }     }      class loadalldata extends asynctask<string, string, string> {           @override         protected void onpreexecute() {             super.onpreexecute();             progressdialog pdialog;              pdialog = new progressdialog(listview.this);             pdialog.setmessage("loading data. please wait...");             pdialog.setindeterminate(false);             pdialog.setcancelable(false);             pdialog.show();             if (pdialog != null && pdialog.isshowing()) pdialog.dismiss();          }          protected string doinbackground(string... args) {              try {                 intent in = getintent();                 string searchterm = in.getstringextra("tag_search");                 string query = urlencoder.encode(searchterm, "utf-8");                 string url = "http://answers.yahooapis.com/answerservice/v1/questionsearch?appid=yahoodemo&query="+ query +"search_in=question&sort=relevance&results=25&output=json&callback=ws_results";                 jsonparsser jparser = new jsonparsser();                 jsonobject jobj = jparser.readjsonfeed(url);                 try {                     results = jobj.getjsonarray(tag_results);                      for(int = 0; < results.length(); i++) {                            jsonobject r = results.getjsonobject(i);                             jsonobject question = r.getjsonobject(tag_question);                            string subject = question.getstring(tag_question_subject);                            string numanswers = question.getstring(tag_question_numanswers);                            string chosenanswers= question.getstring(tag_question_chosenanswer);                            string content = question.getstring(tag_question_content);                             //jsonobject answers = question.getjsonobject(tag_answers);                            //jsonobject answer = answers.getjsonobject(tag_answer);                            //string content1 = answers.getstring(tag_answers_content);                               questionlist = new arraylist<hashmap<string, string>>();                                 hashmap<string, string> map = new hashmap<string, string>();                                 map.put(tag_question_subject, subject);                                map.put(tag_question_numanswers, numanswers);                                 questionlist.add(map);                             }                    } catch (jsonexception e) {                     // todo auto-generated catch block                     e.printstacktrace();                 }             } catch (unsupportedencodingexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }                  return tag_results ;             }         @override         protected void onpostexecute(string file_url) {                  listadapter adapter = new simpleadapter(getbasecontext(), questionlist,                         r.layout.listview,                         new string[] { tag_question_subject, tag_question_numanswers }, new int[] {                         r.id.subject, r.id.numanswers });                  setlistadapter(adapter);                  android.widget.listview lv = getlistview();                  lv.setonitemclicklistener(new onitemclicklistener() {                      @override                     public void onitemclick(adapterview<?> arg0, view arg1,int arg2, long arg3) {                         // todo auto-generated method stub                      }                     });               }}      } 

jsonparsser:

public class jsonparsser {      inputstream is;     jsonobject jobj;     string json = "";     public edittext et;      public jsonparsser () {     }      public jsonobject readjsonfeed(string url) {          try{         httpclient client = new defaulthttpclient();         httppost request = new httppost(url);         //request.seturi(website);         try {             httpresponse response = client.execute(request);         httpentity httpentity = response.getentity();         = httpentity.getcontent();          } catch (clientprotocolexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         try {             bufferedreader reader = new bufferedreader(new inputstreamreader(                     is, "iso-8859-1"), 8);             stringbuilder sb = new stringbuilder();             string line;             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             json = sb.tostring();         } catch (exception e) {             log.e("buffer error", "error converting result " + e.tostring());         }          try {             jobj = new jsonobject(json);         } catch (jsonexception e) {            log.e("json parser", "error parsing data " + e.tostring());         }          log.d("json string",json);          return jobj;          }finally{}      }{     }} 

this printed json string , if understand returning error page web service:

08-02 20:02:27.535: d/json string(795): <!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> 08-02 20:02:27.535: d/json string(795): <html><head><title>yahoo! - 404 not found</title><style> 08-02 20:02:27.535: d/json string(795): /* nn4 hide */  08-02 20:02:27.535: d/json string(795): /*/*/ 08-02 20:02:27.535: d/json string(795): body {font:small/1.2em arial,helvetica,clean,sans-serif;font:x-small;text-align:center;}table {font-size:inherit;font:x-small;} 08-02 20:02:27.535: d/json string(795): html>body {font:83%/1.2em arial,helvetica,clean,sans-serif;}input {font-size:100%;vertical-align:middle;}p, form {margin:0;padding:0;} 08-02 20:02:27.535: d/json string(795): p {padding-bottom:6px;margin-bottom:10px;}#doc {width:48.5em;margin:0 auto;border:1px solid #fff;text-align:center;}#ygma {text-align:right;margin-bottom:53px} 08-02 20:02:27.535: d/json string(795): #ygma img {float:left;}#ygma div {border-bottom:1px solid #ccc;padding-bottom:8px;margin-left:152px;}#bd {clear:both;text-align:left;width:75%;margin:0 auto 20px;} 08-02 20:02:27.535: d/json string(795): h1 {font-size:135%;text-align:center;margin:0 0 15px;}legend {display:none;}fieldset {border:0 solid #fff;padding:.8em 0 .8em 4.5em;} 08-02 20:02:27.535: d/json string(795): form {position:relative;background:#eee;margin-bottom:15px;border:1px solid #ccc;border-width:1px 0;} 08-02 20:02:27.535: d/json string(795): #s1p {width:15em;margin-right:.1em;} 08-02 20:02:27.535: d/json string(795): form span {position:absolute;left:70%;top:.8em;}form {font:78%/1.2em arial;display:block;padding-left:.8em;white-space:nowrap;background: url(http://l.yimg.com/a/i/s/bullet.gif) no-repeat left center;}  08-02 20:02:27.535: d/json string(795): form .sep {display:none;}.more {text-align:center;}#ft {padding-top:10px;border-top:1px solid #999;}#ft p {text-align:center;font:78% arial;} 08-02 20:02:27.535: d/json string(795): /* end nn4 hide */ 08-02 20:02:27.535: d/json string(795): </style></head> 08-02 20:02:27.535: d/json string(795): <body><div id="doc"> 08-02 20:02:27.535: d/json string(795): <div id="ygma"><a href="http://us.rd.yahoo.com/404/*http://www.yahoo.com"><img 08-02 20:02:27.535: d/json string(795): src=http://l.yimg.com/a/i/yahoo.gif 08-02 20:02:27.535: d/json string(795): width=147 height=31 border=0 alt="yahoo!"></a><div><a 08-02 20:02:27.535: d/json string(795): href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">yahoo!</a> 08-02 20:02:27.535: d/json string(795):  - <a href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">help</a></div></div> 08-02 20:02:27.535: d/json string(795): <div id="bd"><h1>sorry, page requested not found.</h1> 08-02 20:02:27.535: d/json string(795): <p>please check url proper spelling , capitalization. if 08-02 20:02:27.535: d/json string(795): you're having trouble locating destination on yahoo!, try visiting 08-02 20:02:27.535: d/json string(795): <strong><a 08-02 20:02:27.535: d/json string(795): href="http://us.rd.yahoo.com/404/*http://www.yahoo.com">yahoo! home 08-02 20:02:27.535: d/json string(795): page</a></strong> or through list of <strong><a 08-02 20:02:27.535: d/json string(795): href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/docs/family/more/">yahoo!'s 08-02 20:02:27.535: d/json string(795): online services</a></strong>. also, may find you're looking 08-02 20:02:27.535: d/json string(795): if try searching below.</p> 08-02 20:02:27.535: d/json string(795): <form name="s1" action="http://us.rd.yahoo.com/404/*-http://search.yahoo.com/search"><fieldset> 08-02 20:02:27.535: d/json string(795): <legend><label for="s1p">search web</label></legend> 08-02 20:02:27.535: d/json string(795): <input type="text" size=30 name="p" id="s1p" title="enter search terms here"> 08-02 20:02:27.535: d/json string(795): <input type="submit" value="search"> 08-02 20:02:27.535: d/json string(795): <span><a href="http://us.rd.yahoo.com/404/*http://search.yahoo.com/search/options?p=">advanced search</a> <span class=sep>|</span> <a href="http://us.rd.yahoo.com/404/*http://buzz.yahoo.com">most popular</a></span> 08-02 20:02:27.535: d/json string(795): </fieldset></form> 08-02 20:02:27.535: d/json string(795): <p class="more">please try <strong><a 08-02 20:02:27.535: d/json string(795): href="http://us.rd.yahoo.com/404/*http://help.yahoo.com">yahoo! 08-02 20:02:27.535: d/json string(795): central</a></strong> if need more assistance.</p> 08-02 20:02:27.535: d/json string(795): </div><div id="ft"><p>copyright &copy; 2013 yahoo! inc. 08-02 20:02:27.535: d/json string(795): rights reserved. <a 08-02 20:02:27.535: d/json string(795): href="http://us.rd.yahoo.com/404/*http://privacy.yahoo.com">privacy 08-02 20:02:27.535: d/json string(795): policy</a> - <a 08-02 20:02:27.535: d/json string(795): href="http://us.rd.yahoo.com/404/*http://docs.yahoo.com/info/terms/">terms 08-02 20:02:27.535: d/json string(795): of service</a></p></div> 08-02 20:02:27.535: d/json string(795): </div></body></html> 

your url:

http://answers.yahooapis.com/answerservice/v1/questionsearch 

the correct url:

http://answers.yahooapis.com/answersservice/v1/questionsearch 

notice difference answerservice should answersservice. have error here:

...&query="+ query +"search... 

should be

...&query="+ query +"&search... 

because query parameters have delimited ampersand. here example of url in action. note need own app id because id gave me error. looks you're using demo app id provided yahoo examples.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -