gpt4 book ai didi

android - 找不到文件异常

转载 作者:行者123 更新时间:2023-11-30 04:24:58 25 4
gpt4 key购买 nike

我在安卓系统工作。我想将 foursquare 与我的应用程序集成。

用于在某个地方签到的功能。我正在使用以下代码:-

    URL url = new URL("https://api.foursquare.com/v2/checkins/add/");

URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();

BufferedReader rd = new BufferedReader(new

InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {

}

但这是生成文件未找到异常。请帮我看看我犯了什么错误。

提前致谢。

最佳答案

尝试以下方法

从 URL 读写数据

void readAndWriteFromWeb(){

//make connection

URL url = new URL("https://api.foursquare.com/v2/checkins/add/");

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setAllowUserInteraction(true);
httpURLConnection.setRequestProperty("Connection", "keep-alive");
httpURLConnection.setRequestProperty("ConnectionTimeout", "12000");
httpURLConnection.setRequestProperty("Content-Length", "" + request.length);

//write data
OutputStream out = httpURLConnection.getOutputStream();
out.write(request);
out.flush();
//Log.e("Request URL "+url, "Request Data "+request);

//read data
InputStream inputStream = httpURLConnection.getInputStream();
int length = httpURLConnection.getContentLength();
//Log.e("Content Length", "" + length);

int readLength = 0;
int chunkSize = 1024;
int readBytes = 0;
byte[] data = new byte[chunkSize];

StringBuilder builder = new StringBuilder();

while((readBytes = inputStream.read(data)) != -1){
builder.append(new String(data,0,readBytes).trim());
readLength += readBytes;

//Release the memory.
data = null;
//Check the remaining length
if((length - readLength) < chunkSize){
if((length - readLength) == 0){
break;
}
data = new byte[((length) - readLength)];
}else{
data = new byte[chunkSize];
}
}
}

关于android - 找不到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8666315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com