2016-04-25 174 views
1

我正在使用IntentServices將數據發佈到服務器,但我不知道如何在IntentService中獲得json格式的響應,以及我們如何能夠發現這個IntentService在後臺運行,並根據響應執行某些操作。如何從android中的Intent服務中的服務器獲取Json響應?

public class CDealIntentService extends IntentService { 
private static final String s_szDealDetailUrl = "http://202.121.144.133:8080/ireward/rest/json/metallica/getDealDetailInJSON"; 
private static String s_szresult = ""; 
Context ctx; 
String m_szMobileNumber, m_szEncryptedPassword; 

/** 
* Creates an IntentService. Invoked by your subclass's constructor. 
* <p/> 
* // * @param name Used to name the worker thread, important only for debugging. 
*/ 
public CDealIntentService() { 
    super("CDealIntentService"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    Context ctx = getApplicationContext(); 
    CLoginSessionManagement m_oSessionManagement = new CLoginSessionManagement(ctx); 
    HashMap<String, String> user = m_oSessionManagement.getLoginDetails(); 
    m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE);// getting mobile from saved preferences.......... 
    m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD);// getting password from shared preferences... 

    InputStream inputStream; 
    try { 
     // 1. create HttpClient 
     HttpClient httpclient = new DefaultHttpClient(); 
     // 2. make POST request to the given URL 
     HttpPost httpPost = new HttpPost(s_szDealDetailUrl); 
     String json; 
     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject(); 
     jsonObject.put("agentCode", m_szMobileNumber);// mobile Number 
     jsonObject.put("pin", m_szEncryptedPassword);// password 
     //noinspection AccessStaticViaInstance 
     jsonObject.put("dealcode", CDealCode.getInstance().getS_szDealCode());// dealCode 
     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 
     // 5. set json to StringEntity 
     StringEntity se = new StringEntity(json); 
     // 6. set httpPost Entity 
     httpPost.setEntity(se); 
     // 7. Set some headers to inform server about the type of the content 
     // httpPost.setHeader("Accept", "application/json"); ///not required 
     httpPost.setHeader("Content-type", "application/json"); 
     // 8. Execute POST request to the given URL 
     HttpResponse httpResponse = httpclient.execute(httpPost); 
     HttpEntity entity = httpResponse.getEntity(); 
     // 9. receive response as inputStream 
     inputStream = entity.getContent(); 
     System.out.print("InputStream...." + inputStream.toString()); 
     System.out.print("Response...." + httpResponse.toString()); 

     StatusLine statusLine = httpResponse.getStatusLine(); 
     System.out.print("statusLine......" + statusLine.toString()); 
     ////Log.d("resp_body", resp_body.toString()); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
      // 10. convert inputstream to string 
      s_szresult = CJsonsResponse.convertInputStreamToString(inputStream); 
     } else 
      s_szresult = "Did not work!"; 

    } catch (Exception e) { 
     Log.d("InputStream", e.getLocalizedMessage()); 
    } 

    System.out.println("Result" + s_szresult); 
} 

}下面的代碼

+0

你可以採取一些SharedPreference變量爲和可檢查服務是否正在運行與否。否則有幾種方法檢查服務是否正在運行 –

+0

其他查詢 – niraj

+0

其他查詢是什麼意思? –

回答

0

使用,

if (statusCode == 200) { 

    String jsonString = EntityUtils.toString(httpResponse.getEntity()); 

    JSONObject jsonObj = new JSONObject(jsonString); 

    // Do operation on jsonObj 
} 
+0

我沒有告訴從服務獲得結果,如何獲得服務的結果 – niraj

+0

請詳細解釋 –

+0

我說的是,不是使用Asynktask,而是使用Intentservice以上述方式將數據以json格式發送到服務器。問題是我不知道如何在Intentservice中的json中獲得響應。 – niraj

相關問題