我有一个叫做的方法hostPhoto(); 它基本上将图像上传到网站并检索链接.然后,我有另一种方法将链接发布到网站.
现在我使用这种方法的方式是这样的:
String link = hostPhoto(); //returns a link in string format
post(text+" "+link); // posts the text + a link.
Run Code Online (Sandbox Code Playgroud)
我的问题是... hostPhoto()上传和检索链接需要几秒钟,我的程序似乎不等待并继续发布,因此我留下链接为null,
无论如何,我可以让它首先获得链接...然后发布?喜欢某种onComplete?或类似的东西..我认为上面的方法可以工作,但通过做Log.i's似乎链接返回到一秒左右后的字符串.
更新:这是我的问题的更新进度,我使用AsyncTask作为通知,但Log.i的错误输出显示urlLink为空...这意味着从hostphoto请求的链接永远不会回来的时间为日志. .
更新2:最终工作!问题是hostPhoto()中的线程,是否有人可以为我提供一个探索,为什么该线程会导致这个?感谢所有回复的人.
private class myAsyncTask extends AsyncTask<Void, Void, Void> {
String urlLink;
String text;
public myAsyncTask(String txt){
text=txt;
}
@Override
protected Void doInBackground(Void... params) {
urlLink=hostPhoto();
//Log.i("Linked", urlLink);
return null;
}
@Override
protected void onPostExecute(Void result) {
try {
Log.i("Adding to status", urlLink);
mLin.updateStatus(text+" "+urlLink);
Log.i("Status:", urlLink);
} catch (Exception e) …Run Code Online (Sandbox Code Playgroud) 我需要创建一个最初检查Android应用程序试用期的服务器,所以
在某种程度上它就像一个Android定时炸弹.
我完全不熟悉服务器,只是想要一个基本的,安全的服务器,只需注册一个电话并将其与试用限制进行比较.
任何人都有关于我如何做到这一点的任何信息?任何代码示例或教程都非常感谢.
我尝试了不同的方法来上传和检索链接通过imgur,但没有成功,尽管看着imgur api. http://api.imgur.com/examples#uploading_java
但以下方法部分工作..我试图检索,
错误:如果发生任何错误.链接到图像:图像托管删除链接的链接:删除托管图像的链接
但我最终只有"删除链接",因为其他人是空白,检查出来:
public void post(String path) {
List<NameValuePair> postContent = new ArrayList<NameValuePair>(2);
postContent.add(new BasicNameValuePair("key", DEV_KEY));
postContent.add(new BasicNameValuePair("image", path));
String url = "http://api.imgur.com/2/upload";
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < postContent.size(); index++) {
if(postContent.get(index).getName().equalsIgnoreCase("image")) {
// If the key equals to "image", we use FileBody to transfer the data
entity.addPart(postContent.get(index).getName(), new FileBody(new File (postContent.get(index).getValue())));
} else {
// …Run Code Online (Sandbox Code Playgroud)