2014-05-21 113 views
-1

即時通訊嘗試從我的網站讀取字符串。該網站返回與PHP呼應會話ID。我只是想讀會話ID之前,我的onCreate與後臺作業調用JSON解析方法從從URL讀取Android字符串

Android JSON Parsing

()函數。必須有一個簡單的函數來從網站讀取一個字符串。希望有人能幫助我。 非常感謝

回答

0
try { 
    URL url = new URL("http://www.vogella.com"); 
    HttpURLConnection con = (HttpURLConnection) url 
    .openConnection(); 
    readStream(con.getInputStream()); 
    } catch (Exception e) { 
    e.printStackTrace(); 
} 



private void readStream(InputStream in) { 
    BufferedReader reader = null; 
    try { 
    reader = new BufferedReader(new InputStreamReader(in)); 
    String line = ""; 
    while ((line = reader.readLine()) != null) { 
     System.out.println(line); 
    } 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    if (reader != null) { 
     try { 
     reader.close(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 
    } 
} 

參考http://www.vogella.com/tutorials/AndroidNetworking/article.html

+0

我應該在哪裏打第一個街區?在onCreate()? – user3659544

+0

no no no。你不應該在onCreate上做任何網絡活動。閱讀關於AsyncTask並在asyc任務中執行網絡活動,一旦你得到你需要的更新UI – nizammoidu

+0

好吧,我明白了。我可以調用這個和JSON解析一樣的AsyncTask嗎? – user3659544

0

試試這個

 public class check extends AsyncTask<String , Void, Integer>{ 
     private ProgressDialog dialog1; 
     protected void onPreExecute() { 
      dialog1 = ProgressDialog.show(currentclass.this, "", "Wait......."); 

     } 

     protected void onPostExecute(Integer feed) { 
      dialog1.dismiss();    
      super.onPostExecute(feed); 
      }  
     protected Integer doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      System.out.println(" do in abckground"); 
      String URL_request = "http://your url"; 
      String response = MyWebService.methodForAsynckTask(URL_request); 
     } 
     } 

調用此爲

 new checkUpdateVersion().execute(); 

然後

 public static String methodForAsynckTask(String URL){ 
    try{ 
     String responseMessage = ""; 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpParams params = httpclient.getParams(); 

    HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT); 
    HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT); 
    ConnManagerParams.setTimeout(params, WAIT_TIMEOUT); 

    HttpGet httpGet = new HttpGet(URL); 
    HttpResponse response = httpclient.execute(httpGet); 
    System.out.println("response forn server :"+response); 
    StatusLine statusLine = response.getStatusLine(); 
    if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     response.getEntity().writeTo(out); 
     out.close(); 
     responseMessage = out.toString(); 
     System.out.println("resonse message :"+responseMessage); 
     return responseMessage; 
    } 

    else{ 
     Log.w("HTTP1:",statusLine.getReasonPhrase()); 
     response.getEntity().getContent().close(); 
     throw new IOException(statusLine.getReasonPhrase()); 
    } 

} catch (ClientProtocolException e) { 
    Log.w("HTTP2:",e); 
    responseMessage = e.getMessage(); 
} catch (IOException e) { 
    Log.w("HTTP3:",e); 
    responseMessage = e.getMessage(); 
}catch (Exception e) { 
    Log.w("HTTP4:",e); 
    responseMessage = e.getMessage(); 
} 
    return responseMessage; 

}