java - 从 BufferedReader 获取 null 返回

标签 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

但是如果我将其初始化为

""

那么返回的是

: null

我将行更改为

String all = null;

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

    return all;

我得到了返回

null : {"data":{"error":"Malformed auth header","request":"/3/image","parameters":"image = iVBORw0KGgoAAAANSUhEUgAAB4AAAASwCAIAAACVUsChAACAAElEQVR42uzdCXebyrI2YEuyY8fzPCbZOyfZd597v///...","method":"POST"},"success":false,"status":403}

最佳答案

在每次迭代中,您都会阅读两行而不是一行。改变

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

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

关于java - 从 BufferedReader 获取 null 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390870/

相关文章:

java - 在 App Engine 上将实体转换为它的类类型

java - 纠正java中解析的URL

java - 标记界面还是注解?

java - 如何在netbeans gui编辑器中将JPanel分配给JFrame?

go - Golang Bufio WriteByte未写入

java - 如何在没有 BufferedReader 的情况下从 Java 中的 http get 请求检索响应

java - 如何在Java中读取压缩的.wav音频文件?

java - Iterator.next() 上的 ConcurrentModificationException

java - 读取二进制流直到遇到 "\r\n"

scala - Stream Continually 只读取 1 分钟