gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:57:58 26 4
gpt4 key购买 nike

我正在使用 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);
}

最佳答案

使用下面的代码,

if (statusCode == 200) {

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

JSONObject jsonObj = new JSONObject(jsonString);

// Do operation on jsonObj
}

关于android - 如何在 android 的 Intent Service 中从服务器获取 Json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36839022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com