Skip to content

Commit

Permalink
Merge pull request #170 from di72nn/devel
Browse files Browse the repository at this point in the history
Fix NPE
  • Loading branch information
tcitworld committed Dec 13, 2015
2 parents 02fa01b + 077872c commit 5327344
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.facebook.stetho.okhttp.StethoInterceptor;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
Expand Down Expand Up @@ -64,6 +65,18 @@ public static Request.Builder getRequestBuilder() {
return b;
}

public static Request getRequest(HttpUrl url) {
return getRequestBuilder().url(url).build();
}

public static HttpUrl getHttpURL(String url) throws IOException {
HttpUrl httpUrl = HttpUrl.parse(url);

if(httpUrl == null) throw new IOException("Incorrect URL");

return httpUrl;
}

public static boolean isNetworkOnline() {
ConnectivityManager cm = (ConnectivityManager) App.getInstance()
.getSystemService(Context.CONNECTIVITY_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.App;

import static fr.gaulupeau.apps.Poche.network.WallabagConnection.getHttpURL;
import static fr.gaulupeau.apps.Poche.network.WallabagConnection.getRequestBuilder;
import static fr.gaulupeau.apps.Poche.network.WallabagConnection.getRequest;

/**
* @author Victor Häggqvist
Expand Down Expand Up @@ -78,66 +80,53 @@ public FeedsCredentials getCredentials() throws IOException {
}

public boolean addLink(String link) throws IOException {
HttpUrl url = HttpUrl.parse(endpoint)
HttpUrl url = getHttpURL(endpoint)
.newBuilder()
.setQueryParameter("plainurl", link)
.build();

Request request = getRequestBuilder()
.url(url)
.build();

return executeRequest(request);
return executeRequest(getRequest(url));
}

public boolean toggleArchive(int articleId) throws IOException {
HttpUrl url = HttpUrl.parse(endpoint)
HttpUrl url = getHttpURL(endpoint)
.newBuilder()
.setQueryParameter("action", "toggle_archive")
.setQueryParameter("id", Integer.toString(articleId))
.build();

Request request = getRequestBuilder()
.url(url)
.build();

return executeRequest(request);
return executeRequest(getRequest(url));
}

public boolean toggleFavorite(int articleId) throws IOException {
HttpUrl url = HttpUrl.parse(endpoint)
HttpUrl url = getHttpURL(endpoint)
.newBuilder()
.setQueryParameter("action", "toggle_fav")
.setQueryParameter("id", Integer.toString(articleId))
.build();

Request request = getRequestBuilder()
.url(url)
.build();

return executeRequest(request);
return executeRequest(getRequest(url));
}

public boolean deleteArticle(int articleId) throws IOException {
HttpUrl url = HttpUrl.parse(endpoint)
HttpUrl url = getHttpURL(endpoint)
.newBuilder()
.setQueryParameter("action", "delete")
.setQueryParameter("id", Integer.toString(articleId))
.build();

Request request = getRequestBuilder()
.url(url)
.build();

return executeRequest(request);
return executeRequest(getRequest(url));
}

public int testConnection() throws IOException {
// TODO: detect redirects
// TODO: check response codes prior to getting body

String url = endpoint + "/?view=about";
Request testRequest = getRequestBuilder().url(url).build();
HttpUrl httpUrl = HttpUrl.parse(endpoint + "/?view=about");
if(httpUrl == null) {
return 6;
}
Request testRequest = getRequest(httpUrl);

Response response = exec(testRequest);
if(response.code() == 401) {
Expand Down Expand Up @@ -177,8 +166,8 @@ public int testConnection() throws IOException {
return 0;
}

private Request getLoginRequest() {
String url = endpoint + "/?login";
private Request getLoginRequest() throws IOException {
HttpUrl url = getHttpURL(endpoint + "/?login");

// TODO: maybe move null checks somewhere else
RequestBody formBody = new FormEncodingBuilder()
Expand All @@ -193,29 +182,25 @@ private Request getLoginRequest() {
.build();
}

private Request getConfigRequest() {
HttpUrl url = HttpUrl.parse(endpoint)
private Request getConfigRequest() throws IOException {
HttpUrl url = getHttpURL(endpoint)
.newBuilder()
.setQueryParameter("view", "config")
.build();

return getRequestBuilder()
.url(url)
.build();
return getRequest(url);
}

private Request getGenerateTokenRequest() {
HttpUrl url = HttpUrl.parse(endpoint)
private Request getGenerateTokenRequest() throws IOException {
HttpUrl url = getHttpURL(endpoint)
.newBuilder()
.setQueryParameter("feed", null)
.setQueryParameter("action", "generate")
.build();

Log.d(TAG, "getGenerateTokenRequest() url: " + url.toString());

return getRequestBuilder()
.url(url)
.build();
return getRequest(url);
}

private boolean executeRequest(Request request) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ protected void onPostExecute(Integer result) {
result);
break;

case 6:
errorMessage = context.getString(R.string.testConnection_errorMessage6,
result);
break;

default:
errorMessage = context.getString(R.string.testConnection_errorMessage_unknown,
result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private String getFeedUrl(FeedType feedType) {
}

private InputStream getInputStream(String urlStr) throws IOException {
Request request = WallabagConnection.getRequestBuilder().url(urlStr).build();
Request request = WallabagConnection.getRequest(WallabagConnection.getHttpURL(urlStr));

Response response = WallabagConnection.getClient().newCall(request).execute();

Expand Down
135 changes: 0 additions & 135 deletions app/src/main/res/values-en/strings.xml

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/res/values-en/strings.xml
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ GAULUPEAU Jonathan — 2013-2015
<string name="testConnection_errorMessage2">Ошибка входа; проверьте имя пользователя и пароль. Код ошибки: %d</string>
<string name="testConnection_errorMessage3">Ошибка авторизации. Код ошибки: %d</string>
<string name="testConnection_errorMessage5">Ошибка входа; проверьте параметры HTTP-авторизации. Код ошибки: %d</string>
<string name="testConnection_errorMessage6">Некорректный URL. Код ошибки: %d</string>
<string name="testConnection_errorMessage_unknown">Неизвестная ошибка. Код ошибки: %d</string>
<string name="d_testConnection_success_title">Успешно</string>
<string name="d_connectionTest_success_text">Проверка соединения успешно выполнена</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ GAULUPEAU Jonathan — 2013-2015
<string name="testConnection_errorMessage2">Can\'t login; check username and password. Error code: %d</string>
<string name="testConnection_errorMessage3">Authorization-related problems. Error code: %d</string>
<string name="testConnection_errorMessage5">Can\'t login; check HTTP Auth parameters. Error code: %d</string>
<string name="testConnection_errorMessage6">Incorrect URL. Error code: %d</string>
<string name="testConnection_errorMessage_unknown">Unknown error. Error code: %d</string>
<string name="d_testConnection_success_title">Success</string>
<string name="d_connectionTest_success_text">Connection test was successful</string>
Expand Down

0 comments on commit 5327344

Please sign in to comment.