Android 如何使用JSONParser实现上传图像的功能?
我一直在研究Google或Stack Overflow上的许多帖子和教程,没有人真正使用Android 如何使用JSONParser实现上传图像的功能?,android,json,image,upload,Android,Json,Image,Upload,我一直在研究Google或Stack Overflow上的许多帖子和教程,没有人真正使用JSONParser上传图像。我找到的所有代码都是用于网站而不是Android的 如何使用JSONParser上传图像 我希望能够从画廊上传照片,或者用相机拍摄照片,然后直接上传到我的应用程序中 JSONParser类如下所示: public class JSONParser { static InputStream is = null; static JSONObject jObj = n
JSONParser
上传图像。我找到的所有代码都是用于网站而不是Android的
如何使用JSONParser
上传图像
我希望能够从画廊上传照片,或者用相机拍摄照片,然后直接上传到我的应用程序中
JSONParser
类如下所示:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String result = "";
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if(method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串结果=”;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
}
我们可以通过传递参数来实现,但似乎没有人在使用它。实现中是否不可能或存在错误?要将文件(图像、音频等)上载到服务器,您可能需要使用MultipartEntity。在线查找并下载这两个库:httpmime-4.0.jar和apache-mime4j-0.4.jar,并将它们添加到项目中。下面是一个如何使用它的示例:
public void doUpload(File fileToUpload)
{
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost(URL_UPLOAD_HERE);
MultipartEntity entity = new MultipartEntity();
entity.addPart("imgType", new StringBody(imgType));
entity.addPart("imgFile", new FileBody(fileToUpload));
httppost.setEntity(entity);
//------------------ read the SERVER RESPONSE
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
Log.d("UploaderService", statusLine + "");
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity resEntity = response.getEntity();
InputStream content = resEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while (( line = reader.readLine()) != null)
{
Log.i("Debug","Server Response " + line);
// try parse the string to a JSON object
try {
jObj = new JSONObject(line);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
reader.close();
} else {
Log.e(UploaderService.class.toString(), "Failed to upload file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
在服务器端,您可以使用这些实体标识符的名称“imgFile”和“imgType”来检索文件并对其进行处理。请注意,使用这些库,您还可以像我在示例中所做的那样随文件发送另一个参数(将“imgType”字符串附加到实体)
考虑在单独的线程(如AsyncTask)中运行此代码