2017-05-08 96 views
-2

我从服务器获取值并保存到共享首选项并将其检索到字符串中,但值为空任何人都可以告诉我什么是错误的。以及如何比较通道和链接中的值字符串。 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; 
} 

}

+0

检查'link'值是空的或者是有什么价值? –

+0

你可以在logcat的web响应中看到有一个值来自服务器 –

+0

在你的线程内写入你的Sharedpreference link = response.toString(); –

回答

1

试试这个: -

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(); 

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); 

       Log.d("Web response", ":" + link); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    networkThread.start(); 
    try { 
     Thread.currentThread().sleep(100); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 




} 



@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; 
} 
+0

我可以得到价值现在和一个更多的建议如果通过比较存储在通道中的值并更新链接值来改变链接值 –

0

尝试使用下面的代码后editor.commit();: -

editor.apply(); 
+0

这将立即保存并在此之后快速使用它。 –

0

试试这个;

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(); 
        UpdateLinkValue(link); 
        Log.d("Web response", ":" + link); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 
     networkThread.start(); 
     try { 
      Thread.currentThread().sleep(100); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    channel = getPrefLink(MainActivity.this); 
    Log.d(" value",":"+channel); 
    } 

    public void UpdateLinkValue(String link){ 
      String prevLink = getPrefLink(MainActivity.this); 
      if(!prevLink.lenght() > 0 || !prevLink.equals(link)) 
       setPrefLink(MainActivity.this,link); 
    } 
    public void setPrefLink(Context context, String link) { 
      SharedPreferences.Editor editor = getPreferencesEditor(context); 
      editor.putString(mypreference , link);   
      editor.apply(); 
      editor.commit(); 
     } 

public SharedPreferences getPreferences(Context context) { 
     return context.getSharedPreferences(mypreference, Context.MODE_PRIVATE); 
    } 

    public String getPrefLink(Context context) {   
      return getPreferences(context).getString(mypreference , ""); 
     } 

    @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; 
    } 
    } 

如果你做的功能,以节省偏好&得到它,你可以从任何地方调用它们。

+0

getPreferencesEditor(上下文)无法解析它说 –

+0

和更多现在我可以得到价值和更多的建议,如果链接值通过比较存储在频道中的价值并更新它 –