more cleanup, initial async apirequest stuff

This commit is contained in:
Andrew Dolgov 2011-11-22 15:49:21 +03:00
parent d9d778c565
commit 2c12a4b677
6 changed files with 90 additions and 35 deletions

View File

@ -2,14 +2,8 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_weight="1"
android:layout_width="match_parent" android:id="@+id/feeds" android:layout_height="match_parent"></ListView>
</LinearLayout>
<TextView android:id="@+id/no_unread_feeds"
android:visibility="invisible"
android:layout_gravity="center" android:color="#909090"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_unread_feeds"></TextView>
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:id="@+id/no_unread_feeds" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="@string/no_unread_feeds" android:layout_gravity="center" ></TextView>
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/feeds"></ListView>
</FrameLayout>

View File

@ -2,15 +2,8 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/headlines_fragment">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_weight="1" android:background="#fafafa"
android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/headlines"></ListView>
</LinearLayout>
<TextView android:id="@+id/no_headlines"
android:visibility="invisible"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_headlines"></TextView>
android:layout_height="fill_parent" android:id="@+id/headlines_fragment">
<ListView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/headlines" android:background="#fafafa"></ListView>
<TextView android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="@string/no_unread_headlines" android:layout_gravity="center" android:id="@+id/no_unread_headlines"></TextView>
</FrameLayout>

View File

@ -8,14 +8,6 @@
</LinearLayout>
</FrameLayout>
<LinearLayout android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal">
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/linearLayout4" android:layout_weight="1.5">
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/feeds_container" android:layout_weight="1"></LinearLayout>
<ImageView android:paddingLeft="2dip" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_weight="1" android:background="?feedlistDivider" android:paddingRight="2dip"></ImageView>
</LinearLayout>
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/headlines_wrapper" android:layout_weight="0.5" android:orientation="vertical">
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/headlines_container" android:layout_weight="1"></LinearLayout>
<LinearLayout android:visibility="gone" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/article_container" android:layout_weight="1"></LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@ -24,9 +24,6 @@
<string name="login_wrong_password">Login failed: username or password incorrect.</string>
<string name="login_success">Logged in.</string>
<string name="no_unread_feeds">No unread feeds.</string>
<string name="offline">Offline</string>
<string name="online">Online</string>
<string name="synchronizing">Synchronizing...</string>
<string name="no_headlines">No articles found.</string>
<string name="no_unread_headlines">No unread headlines.</string>
<string name="loading_message">Loading, please wait...</string>
</resources>

View File

@ -10,6 +10,7 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
@ -17,10 +18,62 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class ApiRequest {
public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonElement> {
private final String TAG = this.getClass().getSimpleName();
protected String m_sessionId;
protected static final int STATUS_LOGIN_FAILED = 0;
protected static final int STATUS_OK = 1;
protected static final int STATUS_API_DISABLED = 2;
protected static final int STATUS_OTHER_ERROR = 3;
private String m_api;
protected void setApi(String api) {
m_api = api;
}
@Override
protected JsonElement doInBackground(HashMap<String, String>... params) {
Gson gson = new Gson();
String requestStr = gson.toJson(new HashMap<String,String>(params[0]));
Log.d(TAG, ">>> (" + requestStr + ") " + m_api);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(m_api + "/api/");
try {
httpPost.setEntity(new StringEntity(requestStr, "utf-8"));
HttpResponse execute = client.execute(httpPost);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
String response = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
Log.d(TAG, "<<< " + response);
JsonParser parser = new JsonParser();
return parser.parse(response);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/* protected String m_sessionId;
protected String m_apiEndpoint;
protected String m_login;
protected String m_password;
@ -173,5 +226,5 @@ public class ApiRequest {
}
return null;
}
} */
}

View File

@ -72,12 +72,37 @@ public class MainActivity extends Activity {
}
setContentView(R.layout.main);
ApiRequest ar = new ApiRequest();
ar.setApi(m_prefs.getString("ttrss_url", null));
ViewFlipper vf = (ViewFlipper) findViewById(R.id.main_flipper);
HashMap<String,String> loginMap = new HashMap<String,String>() {
{
put("op", "login");
put("user", m_prefs.getString("login", null));
put("password", m_prefs.getString("password", null));
}
};
ar.execute(loginMap);
/* ViewFlipper vf = (ViewFlipper) findViewById(R.id.main_flipper);
if (vf != null) {
vf.showNext();
}
HeadlinesFragment hf = new HeadlinesFragment();
FeedsFragment ff = new FeedsFragment();
ArticleFragment af = new ArticleFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.main, ff);
ft.add(R.id.main, hf);
ft.add(R.id.main, af);
ft.hide(hf);
ft.hide(af);
ft.commit(); */
}
@Override
@ -123,4 +148,5 @@ public class MainActivity extends Activity {
return super.onOptionsItemSelected(item);
}
}
}