Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
// 配置API地址
private static final String API = "YOUR API";
// 要上传的文件路径
public String path =FILE PATH”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// 点击 fab 执行图片上传
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread() {
@Override
public void run() {
verifyStoragePermissions(MainActivity.this); // Android 6.0 要请求用户授权
Bitmap image = BitmapFactory.decodeFile(path);
new UploadImage(image, "test", 1).execute();
}
}.start();
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class UploadImage extends AsyncTask<Void, Void, Void>{
Bitmap image; // 要上传的图片
String name; // 图片文件名
int id;
String content; // 接收返回信息
public UploadImage(Bitmap image, String name, int id){
this.image = image;
this.name = name;
this.id = id;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(), "Uploaded", Toast.LENGTH_SHORT).show();
// 解析返回的json
jsonParser(content);
Toast.makeText(getApplicationContext(), Data + " " + info + " " + code, Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... params) {
// 给图片编码为Base64
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
// 添加请求的参数
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("image", encodedImage));
dataToSend.add(new BasicNameValuePair("name", name));
dataToSend.add(new BasicNameValuePair("id", Integer.toString(id)));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(API);
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse response = client.execute(post);
// 获取识别结果
content = EntityUtils.toString(response.getEntity());
//Log.i("content", content);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
private HttpParams getHttpRequestParams(){
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, 1000 * 30);
HttpConnectionParams.setSoTimeout(httpRequestParams, 1000 * 30);
return httpRequestParams;
}
public String code = "";
public String info = "";
public String Data = "";
// 解析返回的json
public void jsonParser(String resultJsonStr){
JSONObject object = null;
try {
object = (JSONObject) new JSONTokener(resultJsonStr).nextValue();
code = object.getString("code"); // 状态代码, 200表示成功
info = object.getString("info"); // 状态, Success表示成功
if(code.equals("200")){
// 这个retData 就是要的数据,现在里面只有Data一项,未来可以加
JSONObject retData = (JSONObject) new JSONTokener(object.getString("data")).nextValue();
Data = retData.getString("Data"); // 返回的信息
}
Log.i("code", code);
Log.i("info", info);
Log.i("Data", Data);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Android 6.0 请求权限
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment