在 Android 中的 activity 之外执行 API 请求
Perform an API request outside of the activity in Android
问题摘要
我想要标准 Java class 到 return LinkedIn API 结果到当前 Activity.
当前进度
到目前为止,我已经能够通过 Activity 中的 API 调用来完成此操作。
我一直在学习本教程,试图在主页 activity 中分离出 API 功能:https://www.studytutorial.in/linkedin-integration-and-login-in-android-tutorial
private Context activityContext;
public ApiSearchLinkedin(String query, Context activityContextIn) {
// Initialize the progressbar
progress= new ProgressDialog(theActivity);
progress.setMessage("Retrieve data...");
progress.setCanceledOnTouchOutside(false);
progress.show();
this.activityContext=activityContextIn;
linkededinApiHelper(activityContext);
}
public void linkededinApiHelper(Context activityContext){
APIHelper apiHelper = APIHelper.getInstance(activityContext.getApplicationContext());
apiHelper.getRequest(activityContext, url, new ApiListener() {
@Override
public void onApiSuccess(ApiResponse result) {
try {
showResult(result.getResponseDataAsJson());
progress.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onApiError(LIApiError error) {
}
});
}
目前出了什么问题
由于未调用 onApiSuccess(),returned 结果目前仅为空。
我认为可能是问题所在
我的猜测是我没有正确获取 Activity 的上下文,但这只是一个猜测。
非常感谢您的帮助!
为什么不直接使用 AsyncTask?
private class Execute extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler httpHandler = new HttpHandler();
String jsonString = httpHandler.makeServiceCall(jsonUrl);
if (jsonString != null) {
try {
//Parse JSON
} catch (JSONException e) {
Log.i("Error: ", e.getMessage());
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
使用它....
new Execute().execute();
HttpHandler class 是这样的:
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
in.close();
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
问题摘要
我想要标准 Java class 到 return LinkedIn API 结果到当前 Activity.
当前进度
到目前为止,我已经能够通过 Activity 中的 API 调用来完成此操作。
我一直在学习本教程,试图在主页 activity 中分离出 API 功能:https://www.studytutorial.in/linkedin-integration-and-login-in-android-tutorial
private Context activityContext;
public ApiSearchLinkedin(String query, Context activityContextIn) {
// Initialize the progressbar
progress= new ProgressDialog(theActivity);
progress.setMessage("Retrieve data...");
progress.setCanceledOnTouchOutside(false);
progress.show();
this.activityContext=activityContextIn;
linkededinApiHelper(activityContext);
}
public void linkededinApiHelper(Context activityContext){
APIHelper apiHelper = APIHelper.getInstance(activityContext.getApplicationContext());
apiHelper.getRequest(activityContext, url, new ApiListener() {
@Override
public void onApiSuccess(ApiResponse result) {
try {
showResult(result.getResponseDataAsJson());
progress.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onApiError(LIApiError error) {
}
});
}
目前出了什么问题
由于未调用 onApiSuccess(),returned 结果目前仅为空。
我认为可能是问题所在
我的猜测是我没有正确获取 Activity 的上下文,但这只是一个猜测。
非常感谢您的帮助!
为什么不直接使用 AsyncTask?
private class Execute extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler httpHandler = new HttpHandler();
String jsonString = httpHandler.makeServiceCall(jsonUrl);
if (jsonString != null) {
try {
//Parse JSON
} catch (JSONException e) {
Log.i("Error: ", e.getMessage());
}
}
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
使用它....
new Execute().execute();
HttpHandler class 是这样的:
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
in.close();
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}