我知道这是一个愚蠢的问题,但自从我在Android Studio中编写Java以来,已经有很长一段时间了。所以实际上我遇到的问题是我无法从一个类返回到另一个类。我在这里尝试了不同类型的解决方案,但它们都没有奏效。不知道为什么。我没有在xml中显示TextView。
这是MainActivity.java:
public class MainActivity extends ActionBarActivity {
TextView txt_temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
XMLreference();
StringRequest();
}
public void XMLreference(){
txt_temp = (TextView) findViewById(R.id.txt_temp);
}
public void StringRequest(){
WebServiceRequest webService = new WebServiceRequest();
String request = webService.main();
txt_temp.setText(request);
}
}
这是WebServiceRequest.java:
public class WebServiceRequest {
private static final String username = "*********"; // put your Device Cloud username here
private static final String password = "*********"; // put your Device Cloud password here
public String responseContent;
/**
* Run the web service request
*/
public String main() {
HttpsURLConnection conn = null;
try {
// Create url to the Device Cloud server for a given web service request
URL url = new URL("https://devicecloud.digi.com/ws/DataStream/00000000-00000000-********-********/xbee.serialIn");
conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
// Build authentication string
String userpassword = username + ":" + password;
// can change this to use a different base64 encoder
String encodedAuthorization = Base64.encodeToString(userpassword.getBytes(), Base64.DEFAULT).trim();
// set request headers
conn.setRequestProperty("Authorization", "Basic "
+ encodedAuthorization);
InputStream is = conn.getInputStream();
Scanner isScanner = new Scanner(is);
StringBuffer buf = new StringBuffer();
while (isScanner.hasNextLine()) {
buf.append(isScanner.nextLine() + "\n");
}
responseContent = buf.toString();
// add line returns between tags to make it a bit more readable
responseContent = responseContent.replaceAll("><", ">\n<");
// Output response to standard out
// System.out.println(responseContent);
} catch (Exception e) {
// Print any exceptions that occur
e.printStackTrace();
} finally {
if (conn != null)
conn.disconnect();
}
return responseContent;
}
}
我已经尝试创建
public void getResponseContent() {
return responseContent;
}
并通过
获取WebServiceRequest webService = new WebServiceRequest();
String request = webService.getResponsteContent();
但它也没有用。我不确定,但是我犯了一个愚蠢的错误,经过几个小时的编程后,我找不到解决它的线索。
答案 0 :(得分:0)
好吧,我不确定你是否想要一个构造函数,但main()
永远不会被调用,所以responseContent
总是空的。
匹配您的代码剪辑
WebServiceRequest webService = new WebServiceRequest();
String request = webService.getResponsteContent();
将main
中的方法WebServiceRequest
更改为像这样的构造函数
public WebServiceRequest() { .. } // it was String main() before
一旦实例化WebServiceRequest
答案 1 :(得分:0)
Web请求必须从第二个线程完成。
Runnable run = new Runnable() {
@Override
public void run() {
try {
//your main method here
handler.post(new Runnable() {
@Override
public void run() {
//update your textView here
}
});
} catch (Exception e) {
}
}
};
Thread thread = new Thread(run);
thread.start();
此外,您需要移动响应代码并更新到Web请求方法中的textView。必须将对用户界面的更改发布到处理程序。
另一个选择是让你的类扩展IntentService。如果你想检查一下,请看这里。
https://developer.android.com/training/run-background-service/create-service.html