1
我有下面的類:Android的情況下錯誤
package com.somedomain.enigma;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
public class APICall {
private String DEBUG_TAG = "enigma";
public void register(Context context, Intent intent) {
final String URL = "http://somedomain.com:8888/register";
String registrationId = intent.getStringExtra("registration_id");
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
// Send a POST to the server to register the device
if (registrationId != null) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("registrationId", registrationId));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();
if(responseCode == 200) {
SharedPreferences settings = context.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("registrationId", registrationId);
editor.commit();
Log.d(DEBUG_TAG, "Device registered successfully");
} else {
Log.d(DEBUG_TAG, "Device registration failed");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (unregistered != null) {
// get old registration ID from shared preferences
// notify 3rd-party server about the unregistered ID
Log.d(DEBUG_TAG, "Unregister request");
}
if (error != null) {
if ("SERVICE_NOT_AVAILABLE".equals(error)) {
// optionally retry using exponential back-off
Log.d(DEBUG_TAG, "Service not available");
} else {
// Unrecoverable error, log it
Log.d(DEBUG_TAG, "Received error: " + error);
}
}
}
}
和我收到這條線上的錯誤"The method getPreferences(int) is undefined for the type Context"
:
SharedPreferences settings = context.getPreferences(Context.MODE_PRIVATE);
我使用Context context = getApplicationContext();
任何想法傳遞方面從以前的方法如何解決這個問題?
啊好吧,我沒看夠,我看到了這個代碼示例,但我想這只是一個活動。 'SharedPreferences sharedPref = getActivity()。getPreferences(Context.MODE_PRIVATE);' – user2004988
@ρяσѕρєяK只有一種活動方法。 –
@ user2004988這只是Activity [class]的一部分(http://developer.android.com/reference/android/app/Activity.html#getPreferences(int))所以是的,你是對的:) –