如何在Android中的Intent Service中从服务器获取Json响应?

3

我正在使用IntentServices将数据发布到服务器,但我不知道如何在IntentService中以json格式获取响应,并且我们如何找出该IntentService是否在后台运行并根据响应执行某些操作。

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);
}

}


你可以使用一些SharedPreference变量来检查Service是否正在运行。否则,还有几种方法可以检查Service是否正在运行。 - Nigam Patro
其他查询怎么样? - niraj
1个回答

1
使用以下代码:
if (statusCode == 200) {

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

    JSONObject jsonObj = new JSONObject(jsonString);

    // Do operation on jsonObj
}

我没有说如何从服务中获取结果,如何在服务中获取结果。 - niraj
请详细解释。 - Sagar Zala
我想说的是,我正在使用Intentservice而不是Asynktask来以json格式将数据发送到服务器。问题是我不知道如何在Intentservice中获取json响应。 - niraj
使用以下代码:String jsonString = EntityUtils.toString(httpResponse.getEntity()); - Sagar Zala
在JSON对象响应中 - niraj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接