資源簡介
實現 NowAPI(http://www.nowapi.com) 的 天氣預報 接口 ,使用 java開發 ,用于 Android。
不完善之處,還請大家不吝賜教。

代碼片段和文件信息
package?com.mutable.bll;
import?java.io.ByteArrayOutputStream;
import?java.io.IOException;
import?java.io.InputStream;
import?java.net.HttpURLConnection;
import?java.net.MalformedURLException;
import?java.net.URL;
import?com.google.gson.JsonSyntaxException;
import?com.mutable.bll.WeatherBuilder.RequestType;
import?android.app.Activity;
import?android.graphics.Bitmap;
import?android.os.AsyncTask;
import?android.text.TextUtils;
public?class?AsyncbaseHttp?extends?AsyncTask?{
private?HttpResponse?listener?=?null;
private?int?requestType;
private?String?url;
private?Activity?activity;
private?String?errorMsg?=?null;
private?String?resultJson?=?““;
private?Bitmap?bitmap?=?null;;
public?AsyncbaseHttp(Activity?activity?String?url?int?requestType
HttpResponse?listener)?{
this.url?=?url;
this.requestType?=?requestType;
this.listener?=?listener;
this.activity?=?activity;
}
@Override
protected?Void?doInBackground(Void...?arg0)?{
try?{
HttpURLConnection?connection?=?(HttpURLConnection)?new?URL(url)
.openConnection();
connection.setRequestMethod(“GET“);
connection.setConnectTimeout(5?*?1000);
connection.setReadTimeout(5?*?1000);
connection.setDoInput(true);
if?(connection.getResponseCode()?==?200)?{
InputStream?is?=?connection.getInputStream();
byte[]?buffer?=?new?byte[1];
ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();
@SuppressWarnings(“unused“)
int?line?=?-1;
while?((line?=?is.read(buffer))?!=?-1)?{?
baos.write(buffer);
}
resultJson?=?baos.toString(“UTF-8“);
byte[]?data?=?baos.toByteArray();
baos.close();
errorMsg?=?Utils.checkResultException(resultJson);
if?(errorMsg?==?null)?{
if?(requestType?==?RequestType.REQUEST_TYPE_WEATHER_ICON)?{
bitmap?=?Utils.genBitmap(data);
}
}
}?else?{
errorMsg?=?“Error:“?+?connection.getResponseCode()?+?“:“
+?connection.getResponseMessage();
}
}?catch?(MalformedURLException?e)?{
e.printStackTrace();
errorMsg?=?“Error:無效的URL“;
}?catch?(IOException?e)?{
e.printStackTrace();
errorMsg?=?“Error:I/O異常“;
}?catch?(JsonSyntaxException?e)?{
e.printStackTrace();
errorMsg?=?“Error:Json語法錯誤“;
}
activity.runOnUiThread(new?Runnable()?{
@Override
public?void?run()?{
if?(errorMsg?==?null)?{
if?(bitmap?!=?null)?{
Utils.responseHandler(requestType?bitmap?listener);
}?else?{
Utils.responseHandler(requestType?resultJson?listener);
}
}?else?{
if?(!TextUtils.isEmpty(errorMsg))?{
errorMsg?=?errorMsg.replace(“Error:“?““);
Utils.responseHandler(RequestType.REQUEST_TYPE_NONE
errorMsg?listener);
}
}
}
});
return?null;
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-03-17?11:13??weather-1.0\
?????文件???????21357??2017-03-17?10:18??weather-1.0\weather-1.0.jar
?????文件???????15698??2017-03-17?11:13??weather-1.0\[API文檔]weather.jar.xlsx
?????目錄???????????0??2017-03-17?10:21??weather-1.0\源碼\
?????目錄???????????0??2017-03-17?10:18??weather-1.0\源碼\src\
?????目錄???????????0??2017-03-17?10:17??weather-1.0\源碼\src\com\
?????目錄???????????0??2017-03-17?10:17??weather-1.0\源碼\src\com\mutable\
?????目錄???????????0??2017-03-17?10:17??weather-1.0\源碼\src\com\mutable\bll\
?????文件????????2861??2017-03-15?17:36??weather-1.0\源碼\src\com\mutable\bll\Asyncba
?????文件?????????946??2017-03-15?16:48??weather-1.0\源碼\src\com\mutable\bll\HttpResponse.java
?????文件????????3273??2017-03-16?08:19??weather-1.0\源碼\src\com\mutable\bll\Utils.java
?????文件?????????455??2017-03-11?14:29??weather-1.0\源碼\src\com\mutable\bll\Weather.java
?????文件????????5709??2017-03-15?19:50??weather-1.0\源碼\src\com\mutable\bll\WeatherBuilder.java
?????目錄???????????0??2017-03-17?10:17??weather-1.0\源碼\src\com\mutable\model\
?????文件????????1143??2017-03-11?10:16??weather-1.0\源碼\src\com\mutable\model\City.java
?????文件?????????512??2017-03-11?14:02??weather-1.0\源碼\src\com\mutable\model\ErrorResponse.java
?????文件????????3414??2017-03-17?10:15??weather-1.0\源碼\src\com\mutable\model\FutureWeather.java
?????文件????????2652??2017-03-17?10:15??weather-1.0\源碼\src\com\mutable\model\HistoryWeather.java
?????文件????????1468??2017-03-17?10:16??weather-1.0\源碼\src\com\mutable\model\PM2_5.java
?????文件?????????364??2017-03-11?14:32??weather-1.0\源碼\src\com\mutable\model\Response.java
?????文件????????3853??2017-03-17?11:08??weather-1.0\源碼\src\com\mutable\model\TodayWeather.java
?????文件?????????575??2017-03-15?16:24??weather-1.0\源碼\src\com\mutable\model\WeatherType.java
- 上一篇:eclipse的js插件
- 下一篇:java實現的注冊功能模塊源代碼
評論
共有 條評論