我是 Android 开发的新手,这是我的第一个应用程序。
这个问题可能很简单,但我找不到解决方案。
此应用程序是一个简单的 ImageViewer,应用程序必须连接到服务器,并且根据某些数据,服务器将返回特定图像。该图像显示在屏幕上,单击它会得到另一个。就是这样!
如我所见,我无法从 UIThread 运行 http post 查询,必须在后台执行。然而,意图是当用户点击图像时。
我有一个 AsyncTask,在创建应用程序时,服务器会识别它,它们会交换一些数据。我m using a library SmartImageViewer (making possible load images from a url).
At the moment in my code I
正在通过计数器 ++ 加载图像,到目前为止它的工作。但是每次用户单击它并在屏幕上显示它时,我都需要获取一个图像 url。
public class MainActivity extends Activity implements OnClickListener {
SmartImageView imageSwitcher;
String SERVER_IP;
String APP_ID;
String DEVICE_ID;
Integer counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB_Queries db = new DB_Queries(getApplicationContext());
Cursor c = db.getServerIPAndAppId();
StringBuilder app_id = new StringBuilder();
StringBuilder server_ip = new StringBuilder();
if (c.moveToFirst()) {
do {
server_ip.append(c.getString(c.getColumnIndex("server_ip")));
app_id.append(c.getString(c.getColumnIndex("app_id")));
} while (c.moveToNext());
}
db.close();
SERVER_IP = server_ip.toString();
APP_ID = app_id.toString();
ServerConnect connect = new ServerConnect();
connect.execute("http://" + server_ip + "/");
SmartImageView image = (SmartImageView) this
.findViewById(R.id.image_switcher);
image.setImageUrl("http://localhost/public/albums/1/_(99).jpg");
imageSwitcher = (SmartImageView) findViewById(R.id.image_switcher);
imageSwitcher.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
if (v.getId() == imageSwitcher.getId()) {
counter++;
Toast.makeText(getApplicationContext(), "Image is clicked",
Toast.LENGTH_LONG).show();
SmartImageView image = (SmartImageView) this
.findViewById(R.id.image_switcher);
image.setImageUrl("http://localhost/public/albums/1/_("
+ counter + ").jpg");
}
}
private class ServerConnect extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String DEVICE_ID = Secure.getString(getContentResolver(),
Secure.ANDROID_ID);
DB_Queries db = new DB_Queries(getApplicationContext());
Cursor c = db.getSettings();
String APP_ID = null;
String INSTALLED_AT = null;
String TOKEN = null;
if (c.moveToFirst()) {
do {
APP_ID = c.getString(c.getColumnIndex("app_id"));
INSTALLED_AT = c
.getString(c.getColumnIndex("installed_at"));
TOKEN = c.getString(c.getColumnIndex("token"));
} while (c.moveToNext());
}
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
post.addHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> pair = new ArrayList<NameValuePair>();
pair.add(new BasicNameValuePair("device_id", DEVICE_ID));
pair.add(new BasicNameValuePair("app_id", APP_ID.toString()));
pair.add(new BasicNameValuePair("token", TOKEN.toString()));
try {
post.setEntity(new UrlEncodedFormEntity(pair));
HttpResponse response = client.execute(post);
InputStream is = response.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder str = new StringBuilder();
String chunk = null;
while ((chunk = br.readLine()) != null) {
str.append(chunk);
}
final String query_response = str.toString().trim();
if (query_response.equals("HH@K2i")) {
Cursor delete = db.updateAuth();
}
} catch (Exception e) {
e.printStackTrace();
}
db.close();
return null;
}
}
}