Java 如何在android中使用新的URLConnection从ajax获取make和HTTP GET

Java 如何在android中使用新的URLConnection从ajax获取make和HTTP GET,java,javascript,android,ajax,http-post,Java,Javascript,Android,Ajax,Http Post,我注意到HtppClient已经被弃用,我想更新一个旧项目,并且我正在启动另一个新项目,所以我不想使用弃用的代码。有人能告诉我在Android中使用URLConnection的新方法吗?我有点不知所措。欢迎对我目前在java中使用的方法进行任何改进。提前谢谢 我主要在javascript中使用ajax命令,并希望在android java中创建这些命令。(API 22+) Ajax代码: $.ajax({ url: 'http://myurl/app_login.php',

我注意到HtppClient已经被弃用,我想更新一个旧项目,并且我正在启动另一个新项目,所以我不想使用弃用的代码。有人能告诉我在Android中使用URLConnection的新方法吗?我有点不知所措。欢迎对我目前在java中使用的方法进行任何改进。提前谢谢

我主要在javascript中使用ajax命令,并希望在android java中创建这些命令。(API 22+)

Ajax代码:

 $.ajax({
     url: 'http://myurl/app_login.php',
     dataType: 'jsonp',
     data: {email:myEmail, password:myPassword}, 
     success: function(data)    {
         callback(data);
     },
     error: function(data)  {
         callback(data);
     }    
 })
我过去常做的事:

final boolean myfunction(String user){
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(getString(R.string.authorization_url));
    String body;
    body = "email=" + user;

    post.setHeader("Content-type","application/x-www-form-urlencoded");

    try {
        post.setEntity(new StringEntity(body,"UTF-8"));
        try {
            HttpResponse response = client.execute(post);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();
            try {
                JSONObject finalResult = new JSONObject(json);

                //do stuff with final result

                return finalResult.getBoolean("success");
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return false;
}
上述代码的相关进口:

import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONException;
    import org.json.JSONObject;
奖金问题寻求答案: 如果有人能告诉我,既然我上传了一个文件,我会非常感激,就像我在下面做的那样。遗憾的是,这些方法/函数中有很多现在已经被弃用了。这与上面的问题类似,但我会上传一个文件,然后等待
try{}
finally()
部分:

文件上载代码:

public String sendUploadFilePOST(FileData fileData, String acc, long parity){

        //setup variables
        String rqid = "FAILED";
        String boundary = "----------------------" + System.currentTimeMillis();
        File thisFile;
        //setup http post
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(getString(R.string.uploadHandler_url));
        HttpResponse response;
        post.addHeader("Content-type", "multipart/form-data; boundary=" + boundary);
        post.addHeader("Accept","*/*");
        post.addHeader("Accept-Encoding", "gzip, deflate");
        post.addHeader("Cache-Control", "no-cache");
        try {

            File tempOutputDir = this.getCacheDir();
            thisFile = new File(tempOutputDir, fileData.fileName);
            OutputStream outputStream = new FileOutputStream(thisFile);
            IOUtils.copy(getContentResolver().openInputStream(fileData.androidUri), outputStream);
            outputStream.close();

            fileData.sizeInBytes = thisFile.length();

            //create multipartentity
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            entityBuilder.setBoundary(boundary);
            //add file
            entityBuilder.addBinaryBody("doc", thisFile);
            //add content values
            entityBuilder.addTextBody("acc", acc);
            entityBuilder.addTextBody("parity", 500000 + "");

            //setup entity
            HttpEntity myMultiEntity = entityBuilder.build();
            post.setEntity(myMultiEntity);

            //execute post
            response = client.execute(post);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return rqid;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return rqid;
        } catch (IOException e) {
            e.printStackTrace();
            return rqid;
        } finally {
            //client.getConnectionManager().shutdown();
            String doNothing = "";
        }


        try {
            //process the response
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            rqid = reader.readLine();

            //--to-do-- think of something if the file wasn't able to be deleted
            boolean fileDeleted = thisFile.delete();

        } catch (IOException e) {
            e.printStackTrace();
        }

        //return rqid value if it is FAILED then the try did not complete fully
        return rqid;
    }
相关进口:

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
提前谢谢。很抱歉代码太多,但如果可以的话,只需复制ajax即可。我只是展示我过去做过的事情和想要改变的地方。

是一个很好的库,它使类似的事情变得简单,并且类似于HttpClient。