从BufferedReader获取null返回

时间:2013-06-30 13:33:31

标签: java bufferedreader

所以我正在尝试使用他们的v3 API(http://api.imgur.com/)将图像上传到Imgur,当我收到回复后,它会返回

null : null

这是我的完整代码。

import java.util.List;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
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.omg.DynamicAny.NameValuePair;
import org.apache.http.NameValuePair;

public class Upload {

    public static void main (String[] args) {

        System.out.println(Imgur("C:\\Users\\wiesa\\Desktop\\image.jpg",     "2313fab45c25337"));
    }

public static String Imgur (String imageDir, String clientID) {
    //create needed strings
    String address = "https://api.imgur.com/3/image";

    //Create HTTPClient and post
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(address);

    //create base64 image
    BufferedImage image = null;
    File file = new File(imageDir);

    try {
        //read image
        image = ImageIO.read(file);
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        ImageIO.write(image, "png", byteArray);
        byte[] byteImage = byteArray.toByteArray();
        String dataImage = new Base64().encodeAsString(byteImage);

        //add header
        post.addHeader("Authorization", "Client-ID" + clientID);
        //add image
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("image", dataImage));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        //execute
        HttpResponse response = client.execute(post);

        //read response
        BufferedReader rd = new BufferedReader(new     InputStreamReader(response.getEntity().getContent()));
        String all = null;

        //loop through response
        while (rd.readLine() != null) {
            all = all + " : " + rd.readLine(); 
        }

        return all;

    }
    catch (Exception e){
        return "error: " + e.toString();
    }
}
}

根据我将字符串“all”初始化的内容,如果我将其设为

null

然后返回

  

null:null

如果我将其初始化为

,则为BUT
""

然后返回

  

:null

我将线路改为

String all = null;

    //loop through response
    String line = null;
    while ((line = rd.readLine()) != null) {
        all = all + " : " + line;
    }

    return all;

我获得了回报

  

null:{“data”:{“error”:“格式错误的auth标题”,“请求”:“/ 3 / image”,“参数”:“image = iVBORw0KGgoAAAANSUhEUgAAB4AAAASwCAIAAACVUsChAACAAElEQVR42uzdCXebyrI2YEuyY8fzPCbZOyfZd597v /// ...”,“方法“:” POST “},” 成功 “:假”,状态“:403}

1 个答案:

答案 0 :(得分:4)

在每次迭代中,您将读取两行而不是一行。变化

while (rd.readLine() != null) { // first line read
    all = all + " : " + rd.readLine(); // second line read
}

String line;
while ((line = rd.readLine()) != null) {
    all = all + " : " + line;
}