我在使用 google plus api 时收到错误 HTTP bad request 。
public class OAuthActivity extends Activity {
private SharedPreferences prefs;
SharedPreferences.Editor prefEdit;
private Person profile;
final static int HTTP_RESPONSE_503 = 503;
String status="status";
String status_value="";
String user_id_key="userid";
String user_id="";
String name_key="name" ;
String name="" ;
String user_type="user_type";
String planidkey="plan_id";
String login_status="";
String username="",passwd="";
String not_valid="Wrong Credentials";
String valid="success";
String plan_id_value="";
boolean status_flag=false;
SQLiteDatabase readfromdb,writeindb;
DatabaseHelper postplanner_dbhelper;
ContentValues values;
int ordervalues;
ProgressDialog pdia;
SharedPreferencesCredentialStore credentialStore;
Dialog type_dilog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
// new OAuthRequestTokenTask(this).execute();
}
protected void onResume() {
super.onResume();
WebView webview = new WebView(this);
webview.setVisibility(View.VISIBLE);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
postplanner_dbhelper=new DatabaseHelper(this);
String googleAuthorizationRequestUrl = new GoogleAuthorizationRequestUrl(
SharedPreferencesCredentialStore.CLIENT_ID,
SharedPreferencesCredentialStore.REDIRECT_URI,
SharedPreferencesCredentialStore.SCOPE).build();
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if (url.startsWith(SharedPreferencesCredentialStore.REDIRECT_URI)) {
try {
if (url.indexOf("code=") != -1) {
// Url is like http://localhost/?code=4/Z5DgC1IxNL-muPsrE2Sjy9zQn2pF
String code = url.substring(
SharedPreferencesCredentialStore.REDIRECT_URI.length() + 7,
url.length());
AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new JacksonFactory(),
SharedPreferencesCredentialStore.CLIENT_ID,
SharedPreferencesCredentialStore.CLIENT_SECRET,
code, SharedPreferencesCredentialStore.REDIRECT_URI).execute();
credentialStore = SharedPreferencesCredentialStore
.getInstance(prefs);
credentialStore.write(accessTokenResponse);
view.setVisibility(View.INVISIBLE);
//*********fatch access token google plus ***************
String _email = fetchGoogleEmail(accessTokenResponse.accessToken);
retrieveProfile();
Log.e("idddd",""+profile);
Log.e("idddd",""+profile.getId());
prefEdit=prefs.edit();
prefEdit.putString("gpuserid", profile.getId());
if(_email!="") {
Log.i("I","AN I AM NOT EMPTY");
prefEdit.putString("gpemail", _email);
} // # end if if mail not empty
prefEdit.commit();
new loginDetails().execute();
//finish();
}else if (url.indexOf("error=") != -1) {
view.setVisibility(View.INVISIBLE);
SharedPreferencesCredentialStore.getInstance(prefs)
.clearCredentials();
showAlertMessage();
}
/*else {
view.setVisibility(View.INVISIBLE);
SharedPreferencesCredentialStore.getInstance(prefs)
.clearCredentials();
startActivity(new Intent(
OAuthActivity.this,
Login_Webservice.class));
finish();
}*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
webview.loadUrl(googleAuthorizationRequestUrl);
}
private void retrieveProfile() {
try {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new NetHttpTransport();
SharedPreferencesCredentialStore credentialStore = SharedPreferencesCredentialStore
.getInstance(prefs);
AccessTokenResponse accessTokenResponse = credentialStore.read();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
accessTokenResponse.accessToken, transport, jsonFactory,
SharedPreferencesCredentialStore.CLIENT_ID,
SharedPreferencesCredentialStore.CLIENT_SECRET,
accessTokenResponse.refreshToken);
Builder b = Plus.builder(transport, jsonFactory).setApplicationName("Kimunita/1.0");
b.setHttpRequestInitializer(accessProtectedResource);
Plus plus = b.build();
profile = plus.people().get("me").execute();
//updateViews();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//***************Google +code Gourav++++++++++++++++++*/
private String fetchGoogleEmail(String token) {
// TODO Auto-generated method stub
if (token == "") {
return "";
}
JSONObject json = null;
String email = "";
String gpUrl = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="
+ token;
Log.e("urll ouathactivity", "" + gpUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(gpUrl);
HttpResponse response;
try {
response = httpClient.execute(httpGet);
Log.e("try", "" + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
Log.e("entity=====", "" + entity);
if (entity != null) {
InputStream instream = entity.getContent();
String jsonstr = convertStreamToString(instream);
Log.e("jsonstr======", "" + jsonstr);
instream.close();
try {
JSONObject jsonObj = new JSONObject(jsonstr);
email = jsonObj.getString("email");
Log.e("email G+ ouathactivity", "" + email);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
json =
genError(response.getStatusLine().getStatusCode(),
response.getStatusLine().toString());
}
}
} catch (ClientProtocolException e) {
try {
json = genError(HTTP_RESPONSE_503,
"Exception: ClientProtocolException");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
try {
json = genError(HTTP_RESPONSE_503, "Exception:IOException");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (JSONException e) {
try {
json = genError(HTTP_RESPONSE_503, "Exception:JSONException");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (Exception e) {
try {
json = genError(HTTP_RESPONSE_503,
"Exception: Exception " + e.toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
httpGet.abort();
}
Log.i("GOOGLE +"," EMAIL ===="+email);
return email;
}
private String fetchDataCorrespondingID(String gpuserid) {
// TODO Auto-generated method stub
JSONObject json = null;
JSONObject json1 = null;
//String gpUrl=AppUtil.server_URL+"/web-service.php?action=GPlogin&id="+gpuserid;
String gp_email=prefs.getString("gpemail", "");
String gpUrl=AppUtil.server_URL+"/web-service.php?action=GPlogin&id="+gp_email;
Log.e("urll ouathactivity",""+gpUrl);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(gpUrl);
HttpResponse response;
try {
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String jsonstr =convertStreamToString(instream);
instream.close();
try
{
JSONObject jsonObj = new JSONObject(jsonstr);
JSONObject data=jsonObj.getJSONObject("response");
status_value=data.getString(status);
// String responseMsg = jsonObj.getString("response");
Log.i("data", data.toString());
if(status_value.equalsIgnoreCase(not_valid))
{
status_flag=false;
}else if(status_value.equalsIgnoreCase(valid))
{
status_value=data.getString(status);
Log.i("status_value", status_value.toString());
user_id=data.getString(user_id_key);
Log.i("user_id", user_id.toString());
name=data.getString(name_key);
Log.i("name", name.toString());
plan_id_value=data.getString(planidkey);
Log.i("plan_id_value", plan_id_value.toString());
status_flag=true;
}else
{
status_flag=false;
pdia.dismiss();
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
else {
json = genError(response.getStatusLine().getStatusCode(), response.getStatusLine().toString());
}
}
}
catch (ClientProtocolException e) {
try {
json = genError(HTTP_RESPONSE_503, "Exception: ClientProtocolException");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
try {
json = genError(HTTP_RESPONSE_503, "Exception: IOException");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (JSONException e) {
try {
json = genError(HTTP_RESPONSE_503, "Exception: JSONException");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (Exception e) {
try {
json = genError(HTTP_RESPONSE_503, "Exception: Exception " + e.toString());
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
httpGet.abort();
}
return status_value;
}
public static JSONObject genError(Integer code, String message) throws JSONException {
// TODO: HTTP-Status (404, etc), for now return status line
String msg = "{error: {code: \"" + code.toString() + "\", message: \"" + message + "\"}}";
Log.e("EEEEEEEEEE",msg );
return new JSONObject("{error: {code: \"" + code.toString() + "\", message: \"" + message + "\"}}");
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
02-11 12:37:34.869: W/System.err(13631): com.google.api.client.http.HttpResponseException: 400 错误请求 02-11 12:37:34.869: W/System.err(13631) : 在 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:669) 02-11 12:37:34.879: W/System.err(13631): 在 com.google.api.client.auth .oauth2.draft10.AccessTokenRequest.executeUnparsed(AccessTokenRequest.java:459) 02-11 12:37:34.879:W/System.err(13631):在 com.google.api.client.auth.oauth2.draft10.AccessTokenRequest。执行(AccessTokenRequest.java:475)02-11 12:37:34.879:W/System.err(13631):在 com.Planner.postplanner.OAuthActivity$1.onPageFinished(OAuthActivity.java:181)02-11 12:37 :34.879: W/System.err(13631): 在 android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:318) 02-11 12:37:34.879: W/System.err(13631): 在 android.os。处理程序。dispatchMessage(Handler.java:99) 02-11 12:37:34.879: W/System.err(13631): 在 android.os.Looper.loop(Looper.java:137) 02-11 12:37:34.879: W/System.err(13631): 在 android.app.ActivityThread.main(ActivityThread.java:4517) 02-11 12:37:34.879: W/System.err(13631): 在 java.lang.reflect.Method .invokeNative(Native Method) 02-11 12:37:34.879: W/System.err(13631): at java.lang.reflect.Method.invoke(Method.java:511) 02-11 12:37:34.879: W/System.err(13631): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 02-11 12:37:34.879: W/System.err(13631): 在 com .android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 02-11 12:37:34.879: W/System.err(13631): at dalvik.system.NativeStart.main(Native Method)循环(Looper.java:137)02-11 12:37:34.879:W/System.err(13631):在 android.app.ActivityThread.main(ActivityThread.java:4517)02-11 12:37:34.879: W/System.err(13631): 在 java.lang.reflect.Method.invokeNative(Native Method) 02-11 12:37:34.879: W/System.err(13631): 在 java.lang.reflect.Method。调用(Method.java:511)02-11 12:37:34.879:W/System.err(13631):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)02-11 12:37:34.879: W/System.err(13631): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 02-11 12:37:34.879: W/System.err(13631 ): 在 dalvik.system.NativeStart.main(Native Method)循环(Looper.java:137)02-11 12:37:34.879:W/System.err(13631):在 android.app.ActivityThread.main(ActivityThread.java:4517)02-11 12:37:34.879: W/System.err(13631): 在 java.lang.reflect.Method.invokeNative(Native Method) 02-11 12:37:34.879: W/System.err(13631): 在 java.lang.reflect.Method。调用(Method.java:511)02-11 12:37:34.879:W/System.err(13631):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)02-11 12:37:34.879: W/System.err(13631): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 02-11 12:37:34.879: W/System.err(13631 ): 在 dalvik.system.NativeStart.main(Native Method)Method.invokeNative(Native Method) 02-11 12:37:34.879: W/System.err(13631): at java.lang.reflect.Method.invoke(Method.java:511) 02-11 12:37:34.879 : W/System.err(13631): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 02-11 12:37:34.879: W/System.err(13631): 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 02-11 12:37:34.879: W/System.err(13631): 在 dalvik.system.NativeStart.main(Native Method)Method.invokeNative(Native Method) 02-11 12:37:34.879: W/System.err(13631): at java.lang.reflect.Method.invoke(Method.java:511) 02-11 12:37:34.879 : W/System.err(13631): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 02-11 12:37:34.879: W/System.err(13631): 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 02-11 12:37:34.879: W/System.err(13631): 在 dalvik.system.NativeStart.main(Native Method)879:W/System.err(13631):在 dalvik.system.NativeStart.main(本机方法)879:W/System.err(13631):在 dalvik.system.NativeStart.main(本机方法)