我从服务器获取值并保存到共享首选项并将其检索到字符串中,但值为空任何人都可以告诉我什么是错误的。以及如何比较通道和链接中的值字符串。 logcat共享首选项值返回空
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
EditText name;
EditText email;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
private static String NAMESPACE = "http://telview360/";
private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
private static String SOAP_ACTION = "http://telview360/ImageDetails";
private static String METHOD_NAME = "ImageDetails";
String link;
String channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Thread networkThread = new Thread() {
@Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
link = response.toString();
Log.d("Web response", ":" + link);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, link);
editor.commit();
Log.d("Saved value",":"+editor.commit());
channel = sharedpreferences.getString(Name, "");
Log.d(" value",":"+channel);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
检查'link'值是空的或者是有什么价值? –
你可以在logcat的web响应中看到有一个值来自服务器 –
在你的线程内写入你的Sharedpreference link = response.toString(); –